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

LocalScript that handles controls stops working once you die?

Asked by 4 years ago

I have a local script inside of StarterPlayerScripts that lets people punch by left clicking. There are zero errors, and when someone dies and respawns, they can't punch anymore. I have no idea why this is happening, but heres the important parts of the code.

UserInputService = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local char = player.CharacterAdded:Wait()
--all my vars

UserInputService.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 and cooldown == false and stunned == false then
--code that makes them punch
    end
end)

char:WaitForChild("Right Arm").Touched:Connect(function(hit)
--code that hurts enemy
    end
end)

char:WaitForChild("Left Arm").Touched:Connect(function(hit)
--code that hurts enemy
    end
end)

Like I said before, there are no errors and everything stops working once you die. Help?

1 answer

Log in to vote
0
Answered by
lolzmac 207 Moderation Voter
4 years ago

Scripts placed inside StarterPlayerScripts do not reset when a character dies. Because the script doesn't reset, the char variable is still pointing to the old character (which is nil) and it is not updating the char variable with the new character that spawns.

You can fix this by using CharacterRemoving and CharacterAdded and editing what the char variable points to, although you would also need to reestablish your events, or by simply moving your script to StarterCharacterScripts, which I would recommend since your script will work as intended with this change.

0
this websites being retarted so can you add me on disc and help me fathawks#7923 fathawks 4 — 4y
Ad

Answer this question