How do i fix this spinning? - lua

The code:
-- yes i know i am using an inefficient loop but if i dont use one everything breaks
local TweenService = game:GetService("TweenService")
while game:GetService("RunService").RenderStepped:Wait()do
local part = script.Parent
local goal = {}
goal.Position = game.Players.LocalPlayer.Character:FindFirstChild("Portal_Gun"):WaitForChild(script.Parent.Name).Position
local goal2 = {}
goal2.Orientation = game.Players.LocalPlayer.Character:FindFirstChild("Portal_Gun"):WaitForChild(script.Parent.Name).Orientation
local tweenInfo = TweenInfo.new(0)
local tween = TweenService:Create(part, tweenInfo, goal)
local tweenInfo2 = TweenInfo.new(.1)
local tween2 = TweenService:Create(part, tweenInfo2, goal2)
tween:Play()
tween2:Play()
end
Note that there is 6 clones of this script and that i am not meaning to break any copyrights
Note 2. The scripts are a descendant of a ViewportFrame
I made an script that uses TweenService which ended up causing a problem
I expected it to work good enough
The problem

Related

FireServer() Doesn't fire the remote event

i was making a script that changes a value when the player dies, i did it with remote events but it didn't work.
Local Script:
local plr = game:GetService('Players').LocalPlayer
wait(2)
local char = plr.Character
local humanoid = char:WaitForChild("Humanoid")
humanoid.Died:Connect(function(death)
print('death')
game:GetService('ReplicatedStorage').setPlayerInLobby:FireServer()
print('fired')
end)
Server script:
rs.setPlayerInLobby.OnServerEvent:Connect(function(plr)
print("works i guess?")
plr.InLobby.Value = true
end)
It printed out "death" and "fired" but it didn't do anything else.
You never defined the rs variable in your server Script.
local rs = game:GetService("ReplicatedStorage")
local plr = game:GetService('Players').LocalPlayer
local char = plr.Character
local humanoid = char:WaitForChild("Humanoid")
if plr.Character.Humanoid.Health == 0 then
print("Bla bla")
end
So you can use it on a local script i guess

Is there a reason why I cannot get a 'pet' to follow me but when just a normal cube part it works with the exact same script? Roblox studio-Lua

This exact code works on a part with a 4,4,4 sizing. I have tried multiple things like adding wields, groups, union. I even changed the sizing to fit the testing. The code should basically make the cube follow the player around when player is walking around. This code works but doesn't function when the same script is parented to the model I want.
local pet = script.Parent
function givePet (player)
if player then
local character = player.Character
if character then
local humRootPart = character.HumanoidRootPart
local newPet = pet:Clone ()
newPet.Parent = character
local bodyPos = Instance.new("BodyPosition", newPet)
bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
local bodyGyro = Instance.new("BodyGyro", newPet)
bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
while wait() do
bodyPos.Position = humRootPart.Position + Vector3.new(2, 2, 3)
bodyGyro.CFrame = humRootPart.CFrame
end
end
end
end
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
givePet(player)
end)
end)
This is the picture of my model that it does not work in
Any advice to solve this issue would be appreciated. Thanks! :D

Can't clone sword into player backpack

Im trying to clone a sword from replicated storage into all players who are in the round's backpack backpack.
code:
for i, v in pairs(game.Workspace.PlayerInRound:GetChildren()) do
local sword = game.ReplicatedStorage.ClassicSword:Clone()
sword.Parent = game.Players.v.Backpack
end
but it just says v is not part of players.
sword.Parent = game.Players.v.Backpack is the same as sword.Parent = game.Players['v'].Backpack, which is definitely senseless. Since v is an object, perhaps, you need sword.Parent = v.Backpack.
What script are you using? If local script, do this.
local player = game.Players.LocalPlayer
local workspaceplayer = game.Workspace:WaitForChild(player.Name)
Sword.Parent = workspaceplayer.BackPack
The workspaceplayer means the player in the workspace.
Script
game.Players.PlayerAdded:Connect(function(player)
Sword.Parent = game.Workspace:WaitForChild(player.Name).BackPack
end)

Leaderstats not working? or just not detecting the click?

I am trying to make a Simulator game on Roblox but I just can't seem to get the leader stats to work or it does work and my click event thing doesn't work, I am just following a tutorial for the scripting so I have no clue. This is my Remotes script where it says print("IS THIS WORKING") that was to see what was the problem. Basically that doesn't run or there's another problem that stops that from running, I think at least. I have other scripts and I will put some in that I think might be neccesary but if you need more feel free to ask me.
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteData = game:GetService("ServerStorage"):WaitForChild("RemoteData")
local cooldown = 1
print("IS THIS WORKING")
replicatedStorage.Remotes.Lift.OnServerEvent:Connect(function(player)
if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
local debounce = remoteData[player.Name].Debounce
if not debounce then
debounce.Value = true
player.leaderstats.Stealth.Value = player.leaderstats.Stealth.Value + 25 *(player.leaderstats.Rebirths.Value + 1)
wait(cooldown)
debounce.Value = false
end
Stats
local serverStorage = game:GetService("ServerStorage")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local stealth = Instance.new("NumberValue")
stealth.Name = "Stealth"
stealth.Parent = leaderstats
local rebirths = Instance.new("IntValue")
rebirths.Name = "Rebirths"
rebirths.Parent = leaderstats
local Folder = Instance.new("Folder")
Folder.Name = player.Name
Folder.Parent = serverStorage.RemoteData
local debounce = Instance.new("BoolValue")
debounce.Name = "Debounce"
debounce.Parent = Folder
end)
ModuleScript
local module = {}
local replicatedStorage = game:GetService("ReplicatedStorage")
function module.Lift()
replicatedStorage.Remotes.Lift:FireServer()
end
return module
LocalScript
local module = require(script.Parent:WaitForChild("ModuleScript"))
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
script.Parent.Activated:Connect(function()
module.Lift()
end)
For my Explorer Structure click
here
local remoteData = game:GetService("ServerStorage"):WaitForChild("RemoteData")
your problem is when it waits for a child it yields the script until RemoteData is found
later in the script, it checks if RemoteData is nil or not
if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
two solutions:
first is to add a max wait for the yield so it will eventually stop if it can't find RemoteData (only useful is RemoteData is added after the script starts running)
second is the replace wait with FindFirstChild that will check immediately without waiting for it
the solution I recommend
local remoteData = game:GetService("ServerStorage"):FindFirstChild("RemoteData")
secondary solution if you have RemoteData added after the script starts and want to double-check
local remoteData = game:GetService("ServerStorage"):WaitForChild("RemoteData",20)
be sure to tell me is this works as I spent a while figuring it out

How to execute a for loop continously instead of getting stuck on a single repeating function?

I'm trying to run a Function for all the Children of a model, but the function essentialy loops infinitely, preventing the next item in the for loop being called, I need either a better way of doing it or a workaround.
The main code:
local wave = workspace["Wave model"]
local move = require(script.Parent.moveModule)
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function()
for _, child in ipairs(wave:GetChildren()) do
move.moveUp(child)
wait(math.random(0.1,0.3))
print("exec")
end
print("executed")
end)
The module I am calling:
local functions = {}
function functions.callNext(part, which)
if which == "up" then
functions.moveUp(part)
else
functions.moveDown(part)
end
end
function functions.moveUp(part)
local Tween = game:GetService("TweenService")
local Objective = {}
Objective.Position = Vector3.new(part.Position.X, part.Position.Y + 10, part.Position.Z)
local tweenInfo = TweenInfo.new(5)
local tweenie = Tween:Create(part, tweenInfo, Objective)
print("played")
tweenie:Play()
print("done")
tweenie.Completed:Connect(function()
print("Fcuk")
end)
wait(5)
functions.callNext(part, "down")
end
function functions.moveDown(part)
local Tween = game:GetService("TweenService")
local Objective = {}
Objective.Position = Vector3.new(part.Position.X, part.Position.Y - 10, part.Position.Z)
local tweenInfo = TweenInfo.new(5)
local tweenie = Tween:Create(part, tweenInfo, Objective)
print("played")
tweenie:Play()
print("done")
wait(5)
functions.callNext(part, "up")
end
return functions
Any ideas?
You don't have an infinite loop, but rather a recursive loop: the functions in the module are calling each other until you will have a stack overflow. Spawning them in different threads will multiply that.
I would instead trigger your direction change by the tween's callback function, that way using asynchronicity:
local functions = {}
function functions.moveUp(part)
local Tween = game:GetService("TweenService")
local Objective = {}
Objective.Position = Vector3.new(part.Position.X, part.Position.Y + 10, part.Position.Z)
local tweenInfo = TweenInfo.new(5)
local tweenie = Tween:Create(part, tweenInfo, Objective)
tweenie:Play()
tweenie.Completed:Connect(function()
functions.moveDown(part)
end)
end
function functions.moveDown(part)
local Tween = game:GetService("TweenService")
local Objective = {}
Objective.Position = Vector3.new(part.Position.X, part.Position.Y - 10, part.Position.Z)
local tweenInfo = TweenInfo.new(5)
local tweenie = Tween:Create(part, tweenInfo, Objective)
tweenie:Play()
tweenie.Completed:Connect(function()
functions.moveUp(part)
end)
end
return functions
Btw -
You should read the docs on the tween service, its very neat. You can use it to go up and down in one run. That way you don't have to toggle with the two functions. Also you can smooth the movements with a sine wave.
What you could do is edit the main script to run the moveup() function on a separate thread like so:
local wave = workspace["Wave model"]
local move = require(script.Parent.moveModule)
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function()
for _, child in ipairs(wave:GetChildren()) do
spawn(function() move.moveUp(child) end) --Using an anonymous function as the spawn() function doesn't accept functions with parameters
wait(math.random(0.1,0.3))
print("exec")
end
print("executed")
end)
This works, however in the long run you shouldn't do this as infinite loops running on multiple threads can cause significant lag if not optimised
Code changed:
move.moveUp(child)
to
spawn(function() move.moveUp(child) end)

Resources