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 5 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.

01UserInputService = game:GetService("UserInputService")
02local player = game:GetService("Players").LocalPlayer
03local char = player.CharacterAdded:Wait()
04--all my vars
05 
06UserInputService.InputBegan:Connect(function(input)
07    if input.UserInputType == Enum.UserInputType.MouseButton1 and cooldown == false and stunned == false then
08--code that makes them punch
09    end
10end)
11 
12char:WaitForChild("Right Arm").Touched:Connect(function(hit)
13--code that hurts enemy
14    end
15end)
16 
17char:WaitForChild("Left Arm").Touched:Connect(function(hit)
18--code that hurts enemy
19    end
20end)

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
5 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 — 5y
Ad

Answer this question