Script problem!

Hello, I have this script that change the color of a block and when I die the script keep in memory my last character position (before to die) !

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local RemoteEvent = ReplicatedStorage.Checkpoint
local Checkpoint = script.Parent
local Level = Checkpoint.Name
local Stage = Checkpoint.Parent.Name
local Part = Checkpoint.Union

Player.CharacterAdded:Connect(function(Character)
	while wait() do
		local Distance = Character.PrimaryPart.Position.Z - Part.Position.Z
		if Distance > 0 then
			Part.Color = Color3.new(0, 1, 0)
		else
			Part.Color = Color3.new(1, 1, 1)
		end
	end
end)
2 Likes
while wait() do
		local Distance = (Character.PrimaryPart.Position - Part.Position).Magnitude
		if Distance > 0 then
			Part.Color = Color3.new(0, 1, 0)
		else
			Part.Color = Color3.new(1, 1, 1)
		end
	end
2 Likes

this code should be placed in game.starterplayer.startercharatr. not in characteradded signal

while wait() do
		local Distance = Character.PrimaryPart.Position.Z - Part.Position.Z
		if Distance > 0 then
			Part.Color = Color3.new(0, 1, 0)
		else
			Part.Color = Color3.new(1, 1, 1)
		end
	end
2 Likes

Thanks you for the reply but that doesn’t, I want to know why the script keep in memory 2 distance ?

My script work well, it doesn’t work only after the character die !
My script is actually here :
BUG

1 Like

like Cairo said, must use Magnitude to comp distance

1 Like

Try this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local RemoteEvent = ReplicatedStorage.Checkpoint
local Checkpoint = script.Parent
local Level = Checkpoint.Name
local Stage = Checkpoint.Parent.Name
local Part = Checkpoint.Union

Player.CharacterAdded:Connect(function(Character)
	while wait() do
		local Distance = (Character.PrimaryPart.Position - Part.Position).Magnitude
		if Distance > 0 then
			Part.Color = Color3.new(0, 1, 0)
		else
			Part.Color = Color3.new(1, 1, 1)
		end
	end
end)

If it fix your problem:

Solution, like Cairo said, you just need to compare the magnitude.
What is magnitude? Magnitude is the distance between 2 parts.
Why probably doesn’t works? You are comparing only the Z distance, you need to compare X, Y and Z

I hope that I helped! :wink:

1 Like

Hi, to prevent this, do this:

local Part = workspace.Size.Part -- The Part i used
local Player = game.Players.LocalPlayer

Usable = true -- Bool (IMPORTANT)

Player.CharacterAdded:Connect(function()
	Usable = true
end)

Player.Character:WaitForChild("Humanoid").Died:Connect(function()
	Usable = false
end)

while wait() and Usable == true do -- The Bool check if Usable (Our Bool) is true
local Mag = (Player.Character.PrimaryPart.Position - Part.Position).Magnitude

if Mag >= 25 then -- Set this to whatever
		Part.Color = Color3.new(0, 1, 0)
	else
		Part.Color = Color3.new(1, 1, 1)
	end
end
if Usable == false then
	Part.Color = Color3.new(1,1,1) -- Resets Part Color
end
2 Likes

Hi @Ankazen @DasKairo @Rodigooz !
Thank you very much for your help but I have already fixed it, here the code :

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Checkpoint = script.Parent
local Part = Checkpoint.Union
local Part2 = Checkpoint.Left
local Part3 = Checkpoint.Right
local Level = Checkpoint.Parent.Level
local CurrentCharacter

Player.CharacterAdded:Connect(function(Character)
	CurrentCharacter = Character
end)

repeat wait() until CurrentCharacter

while wait() do	
	local Distance = CurrentCharacter.PrimaryPart.Position.Z - Part.Position.Z
	local Distance2 = CurrentCharacter.PrimaryPart.Position.X - Part2.Position.X
	local Distance3 = CurrentCharacter.PrimaryPart.Position.X - Part3.Position.X
	if Distance > 0 and Distance2 < 0 and Distance3 > 0 then
		if Part.Color ~= Color3.new(0, 1, 0) then
			Part.Color = Color3.new(0, 1, 0)
			Level.Value += 1
		end
	else
		if Part.Color ~= Color3.new(1, 1, 1) then
			Part.Color = Color3.new(1, 1, 1)
			if Level.Value > 0 then
				Level.Value -= 1
			end
		end
	end
end

If you fixed it, mark your post as the solution.

1 Like

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