Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

2 comments

0

Is this a Local or Server Script?

0

it is a Server Script

1 answer

###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)

This discussion is preserved as it was. New answers, comments, and votes are closed.