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

Is there a way to make this script turn into an OnTouch?

game.Players.PlayerAdded:connect(function(Player)
    Player.CharacterAdded:connect(function(Character)
        local function SetWalkSpeedForPlayers(Players, WalkSpeed)
            for _, Player in pairs(Players) do
                Player.Character:WaitForChild("Humanoid")
                Player.Character.Humanoid.WalkSpeed = WalkSpeed
 
            end
        end
 
        SetWalkSpeedForPlayers(game.Players:GetPlayers(), 0)
        wait(5)
        SetWalkSpeedForPlayers(game.Players:GetPlayers(), 16)
    end)
end)

I've tried many ways but could not succeed to make this script work as a OnTouch. My thought was to put this script inside a part and to make this script be an OnTouch script, and when the part is touched it sets the player speed to 0 and after 5 seconds sets it back to 16. Help?

1 answer

✓ Accepted answer

You could use a table(local Table = {}) and insert the Player into it(table.insert(Table, Player)).

Example:

script.Parent.Touched:connect(function(Touched) -- Once the part is touched
	local Table = {} -- The table
	if Touched.Parent:FindFirstChild("Humanoid") then -- Make sure it's a player
		table.insert(Table, game.Players:FindFirstChild(Touched.Parent.Name)) -- Insert the player into the table
	end -- End the if statement
end) -- End the Touched function

And then replace SetWalkSpeedForPlayers(game.Players:GetPlayers(), 0) and SetWalkSpeedForPlayers(game.Players:GetPlayers(), 16) in your code to SetWalkSpeedForPlayers(Table, 0) and SetWalkSpeedForPlayers(Table, 16).

Example, and full script:

local function SetWalkSpeedForPlayers(Players, WalkSpeed)
	for _, Player in pairs(Players) do
		Player.Character:WaitForChild("Humanoid")
		Player.Character.Humanoid.WalkSpeed = WalkSpeed
	end
end

script.Parent.Touched:connect(function(Touched) -- Once the part is touched
	local Table = {} -- The table
	if Touched.Parent:FindFirstChild("Humanoid") then -- Make sure it's a player
		table.insert(Table, game.Players:FindFirstChild(Touched.Parent.Name)) -- Insert the player into the table
		SetWalkSpeedForPlayers(Table, 0) -- Set the walkspeed for the players in the table
		wait(5)
		SetWalkSpeedForPlayers(Table, 16) -- Set the walkspeed for the players in the table
	end -- End the if statement
end) -- End the Touched function

There probably is a better way to script it, but this way works(or at least, it did for me..)

Sorry I can't explain it better.

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