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

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

Asked by 3 years ago

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)
0
which commands arent working? Killerbot712 47 — 3y

1 answer

Log in to vote
0
Answered by
Leamir 3138 Moderation Voter Community Moderator
3 years ago

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

Answer this question