Fix Script Problems

Second time a player’s character is spwaned
it’s not working

-- It's StarterPlayerScript
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")

local Connect

Player.CharacterAdded:Connect(function(NewChar)
	if Connect then
		Connect:Disconnect()
	end
	OutSideWater()
	Character = NewChar
	HumanoidRootPart = NewChar:WaitForChild("HumanoidRootPart")

	Connect = HumanoidRootPart.Touched:Connect(function(hit)
		if hit.Name == "Water" and not IsFalt then
			IsFalt = true
			InSideWater()
			Humanoid:ChangeState("Physics")
			task.wait(3)
			Event_LoadCharacter:FireServer()
		end
	end)
end)

function InSideWater()
	Sounds.Ambience:Play()
	Sounds.WaterLeave:Play()

	for i= 0, 15 do
		WaterBlur.Size = i
		WaterColor.TintColor = Color3.new(1 - .038 * i, 1 - .027 * i, 1 - .014 * i)
		task.wait()
	end
end

function OutSideWater()
	Sounds.Ambience:Stop()

	for i = 15, 0, 1 do
		WaterBlur.Size = i
		WaterColor.TintColor = Color3.new(1 - .038 * i, 1 - .027 * i, 1 - .014 * i)
		task.wait()
	end
end

Maybe do :findfirstchild(“humanoidrootpart“)

Can you elaborate on what is not working in the script?

1 Like

if Connect then
Connect:Disconnect()
end

after this

You don’t need to disconnect the connection I believe. When your character respawns it doesn’t use that old connection. So try remove the if Connect then
Connect:Disconnect()
end part.

Hello there :wave:

There are some things to be addressed in your code. Let’s start with the “Connect” variable.

It’s not necessary to use the disconnect event as long as removing the instance is handled through the :Remove() method or by ROBLOX internally (when a character respawns). Therefore, you can remove the entire if-statement that checks if the variable is set.

The “IsFalt”-variable seems to be a global variable (which isn’t defined properly). You set it to true once and never touch it again.

Let’s declare the variable at the top of your program along with the rest:

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")

local IsFalt = false -- added the variable for clarity

Now that this is taken care of, we can move on to the main problem.

As mentioned before, you’re setting the “IsFalt” variable only once in your entire program, specifically to “true.” From what I see, you check the variable to be “false” (... and not IsFallen), which only happens once and then never again.

A solution would be to set the variable back to false somewhere in your code.

Without knowing what you’re trying to achieve, it’s difficult to pinpoint exactly where to reset the variable. However, here’s a possible version that might solve your problem.

-- It's StarterPlayerScript
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")

local IsFalt = false -- added the variable for clarity

-- local Connect  -- this can be removed as ROBLOX takes care of the garbage collection

Player.CharacterAdded:Connect(function(NewChar)
	-- if Connect then                  -- this can be removed as ROBLOX takes care of the garbage collection
	-- 	Connect:Disconnect()
	-- end
	OutSideWater()
	Character = NewChar
	HumanoidRootPart = NewChar:WaitForChild("HumanoidRootPart")

	HumanoidRootPart.Touched:Connect(function(hit)
		if hit.Name == "Water" and not IsFalt then
			IsFalt = true
			InSideWater()
			Humanoid:ChangeState("Physics")
			task.wait(3)
			Event_LoadCharacter:FireServer()
		elseif hit.Name ~= "Water" and IsFalt then
			IsFalt = false
		end
	end)
end)

function InSideWater()
	Sounds.Ambience:Play()
	Sounds.WaterLeave:Play()

	for i= 0, 15 do
		WaterBlur.Size = i
		WaterColor.TintColor = Color3.new(1 - .038 * i, 1 - .027 * i, 1 - .014 * i)
		task.wait()
	end
end

function OutSideWater()
	Sounds.Ambience:Stop()

	for i = 15, 0, 1 do
		WaterBlur.Size = i
		WaterColor.TintColor = Color3.new(1 - .038 * i, 1 - .027 * i, 1 - .014 * i)
		task.wait()
	end
end

I really hope this helped you out.
If you have more questions feel free to ask them.

  • DosenSuppe2_0

I wanna ask a question sometimes i use a function that works on server-side like PlayerAdded() but they do not work in client why is it not working?

The client can’t access the server. Basically, some functions can’t work on the client because they are designed to be on server scripts only.

Player Added works on the client by the way

Thank you so much.Have a good day :slight_smile:

The local script runs after you join, so it detects others joining after you, but not yourself. Replace the code with this:

function onPlayerAdded(Plr)
task.wait(5)
print(Plr.Name)
end

game.Players.PlayerAdded:Connect(onPlayerAdded)

onPlayerAdded(game.Players.LocalPlayer)

1 Like

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.