Local script only doesnt work when player joins

–Function
What this script does it if you are wear a certain suit on your torso it would deactivated harmful areas and it does work.

–Problem
Except when you join the game while your wearing the suit already. Meaning you have to take it off and the put it on again for the script to work.

Script is found in StarterPlayerScript.

local plr = game.Players.LocalPlayer
local suitType = plr:WaitForChild("ArmorEquipped")
local torso = suitType.Torso

local warnA = game.Workspace.WarningAreaNotif


torso.Changed:Connect(function (newValue)
	if typeof(newValue) ~= "Instance" then
		
		for i, p in pairs(warnA:GetChildren()) do
			if p.Name == "HazmatAreas" then
				p.CanTouch = true
				p.damageScript.Enabled = true
			end
		end
		
		for i, p in pairs(warnA:GetChildren()) do
			if p.Name == "ColdSuitAreas" then
				p.CanTouch = true
				p.damageScript.Enabled = true
			end
		end
		
		for i, p in pairs(warnA:GetChildren()) do
			if p.Name == "HeatSuitAreas" then
				p.CanTouch = true
				p.damageScript.Enabled = true
			end
		end
		
	else 
		
		if newValue.Name == "Basic Top" then
			
			for i, p in pairs(warnA:GetChildren()) do
				if p.Name == "HazmatAreas" then
					p.CanTouch = false
					p.damageScript.Enabled = false
				end
			end	
		end
		
		if newValue.Name == "Basic Top" then

			for i, p in pairs(warnA:GetChildren()) do
				if p.Name == "ColdSuitAreas" then
					p.CanTouch = false
					p.damageScript.Enabled = false
				end
			end	
		end
		
		if newValue.Name == "Basic Top" then

			for i, p in pairs(warnA:GetChildren()) do
				if p.Name == "HeatSuitAreas" then
					p.CanTouch = false
					p.damageScript.Enabled = false
				end
			end	
		end	
		
	end	
end)

Im sure the problem has the do something with the .Changed at the function.

1 Like

me too i have some similar issues, but for me it doesn’t wan’t to start punch animation for some reason :confused:

Is the torso variable an ObjectValue?

1 Like

This is how it looks in the player
image
image

1 Like

You need to separate the function from the Changed event so you are able to call it when it is first ran

This will call the TorsoUpdated function when that ObjectValue is changed or when the script is first ran which should solve your issue

local plr = game.Players.LocalPlayer
local suitType = plr:WaitForChild("ArmorEquipped")
local torso = suitType.Torso

local warnA = game.Workspace.WarningAreaNotif

local function TorsoUpdated(newValue)
	if typeof(newValue) ~= "Instance" then
		for i, p in pairs(warnA:GetChildren()) do
			if p.Name == "HazmatAreas" then
				p.CanTouch = true
				p.damageScript.Enabled = true
			end
		end

		for i, p in pairs(warnA:GetChildren()) do
			if p.Name == "ColdSuitAreas" then
				p.CanTouch = true
				p.damageScript.Enabled = true
			end
		end

		for i, p in pairs(warnA:GetChildren()) do
			if p.Name == "HeatSuitAreas" then
				p.CanTouch = true
				p.damageScript.Enabled = true
			end
		end

	else 

		if newValue.Name == "Basic Top" then

			for i, p in pairs(warnA:GetChildren()) do
				if p.Name == "HazmatAreas" then
					p.CanTouch = false
					p.damageScript.Enabled = false
				end
			end	
		end

		if newValue.Name == "Basic Top" then

			for i, p in pairs(warnA:GetChildren()) do
				if p.Name == "ColdSuitAreas" then
					p.CanTouch = false
					p.damageScript.Enabled = false
				end
			end	
		end

		if newValue.Name == "Basic Top" then

			for i, p in pairs(warnA:GetChildren()) do
				if p.Name == "HeatSuitAreas" then
					p.CanTouch = false
					p.damageScript.Enabled = false
				end
			end	
		end
	end
end

torso.Changed:Connect(TorsoUpdated)
TorsoUpdated(torso.Value)
1 Like

Thanks for coming up with a solution so fast :slight_smile:

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