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

Local script not working after respawn?

I am having trouble getting this local script to work. It is supposed to push you up whenever you touch any objects called "Wind", but it only works until you die at least once. After you respawn, nothing happens when touching wind. No error, nothing. I tried putting a few prints in, but none of those activated either. Also, the reason I am putting this into a local script instead of a server script every wind object is because I want no delay from the server.

p = game.Players.LocalPlayer
repeat wait() until p.Character
t = p.Character:WaitForChild'Torso'

t.Touched:connect(function(hit)
	if hit and hit.Name == 'Wind' and not(t:FindFirstChild'BodyForce') then
		local force = Instance.new('BodyForce', t)
		force.Force = Vector3.new(0,7400,0)
	end
end)

t.TouchEnded:connect(function(hit)
	if hit and hit.Name == 'Wind' and t:FindFirstChild'BodyForce' then
		t.BodyForce:Destroy()
	end
end)

2 comments

0

Is the script in StarterPlayerScripts?

0

Yes, it is.

1 answer

✓ Accepted answer

##Problem

The code doesn't know you've respawned. Once you die, your character is completely reset and you get a new torso and everything.


##Solution

Look for a respawn. There is an event for Players if there is a new character called CharacterAdded. This event fires everytime the user is respawned or is replaced.


##Final Script

p = game.Players.LocalPlayer
repeat wait() until p.Character --This line becomes useful again! Yay!
--t = p.Character:WaitForChild'Torso' --This line is still no longer needed. Adjustment on line 7.

function CharacterLoading(Char)

    local t = Char:FindFirstChild'Torso' or Char:WaitForChild'Torso'

    t.Touched:connect(function(hit)
        if hit and hit.Name == 'Wind' and not(t:FindFirstChild'BodyForce') then
            local force = Instance.new('BodyForce', t)
            force.Force = Vector3.new(0,7400,0)
        end
    end)

    t.TouchEnded:connect(function(hit)
        if hit and hit.Name == 'Wind' and t:FindFirstChild'BodyForce' then
            t.BodyForce:Destroy()
        end
    end)

end

CharacterLoading(p.Character)
p.CharacterAdded:connect(CharacterLoading)

######Hopefully this answered your question, if it did do not forget to hit the accept answer button. If you have any further questions, feel free to comment below.

3 comments

0

Huh, this script looks like it should definitely work, but now it doesn't do anything at all. This is really strange.

0

You know, it could be the initial loading of the player, let me see if I can revise it. Edit: Updated it, try now.

0

It works perfectly! Thanks!

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