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

Why do some of the commands in the script not activate?

I have a script that used to work. I changed some small stuff with the script and the game and now some of the script doesn't activate when the Parent is touched. The GameMusic is successfully destroyed, but the print() that is above it doesn't work.

local subject = script.Parent
local debounce = true
local player = game.Players.LocalPlayer

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid")and debounce == true then
		debounce = false
		print("player has touched")
		game.SoundService.GameMusic:Destroy()
		player.character.Humanoid.WalkSpeed = 0
		wait(12.5)
		script.Parent.Error.Playing = true
		script.Parent.BrickColor = BrickColor.new(255, 0, 0)
		
	end
end)

1 comment

0

which commands arent working?

1 answer

✓ Accepted answer

LocalPlayer is not defined in server scripts, you can get player by hit.Parent(which should be character)

local subject = script.Parent
local debounce = true

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- try to get player from character
    if player and debounce == true then -- if is a valid player
        debounce = false
        print("player has touched")
        game.SoundService.GameMusic:Destroy()
        player.Character.Humanoid.WalkSpeed = 0 
        wait(12.5)
        script.Parent.Error.Playing = true
        script.Parent.BrickColor = BrickColor.new(255, 0, 0)
        debounce = true -- you forgot to reset debounce, so it can be touched again
    end
end)

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