Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is my script only works in the studio not when i play the game?

Asked by 5 years ago

I'm new to scripting and this script only works in Roblox studio why is that

game.Players.PlayerAdded:connect(function(plr)
    plr.CharacterAdded:connect(function(char)
        local head = char:WaitForChild('Head')
        while wait(4/10) do
            local speed = head.Velocity.Magnitude
            if speed >=4 then
                workspace.Events.addStep:FireServer()
            elseif speed <= 1 then

            end
        end
    end)
end)
0
Is this a Local or Server Script? piRadians 297 — 5y
0
it is a Server Script AdminTheRoblox 0 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

The problem with your script is you're calling FireServer from the server, when it should be called on the client. RemoteEvent.FireClient may be what you're looking for. The purpose of remotes is to let the server and client communicate. How would it make sense, with remotes, for the server to communicate with itself? It doesn't. Changes made by the server replicate to all clients, so you won't need remotes for this.

game.Players.PlayerAdded:Connect(function(plr) -- connect is deprecated
    plr.CharacterAdded:Connect(function(char)
        local head = char.Head

        while head.Velocity.Magnitude >= 4 do -- make it all one condition!
            workspace.Events.addStep:FireClient(plr)
            wait(4/10)
        end
    end)
end)

If you want to use the server event, then simply make a function for it and call it.

local function addStep()
    -- do your code 
end

game.Players.PlayerAdded:Connect(function(plr) -- connect is deprecated
    plr.CharacterAdded:Connect(function(char)
        local head = char.Head

        while head.Velocity.Magnitude >= 4 do -- make it all one condition!
            addStep() -- call function 
            wait(4/10)
        end
    end)
end)
Ad

Answer this question