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)
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
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
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
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
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