Im trying to make a script where the player will pass a potato to the other layer which has been touched, but it only works once. I’ve searched everywhere for the past week and still couldnt find a solution.
Server Script:
coroutine.wrap(function()
HotPotatoHolder.Value.Character:WaitForChild("HumanoidRootPart").Touched:Connect(function(hit)
if hit:IsA("BasePart") and hit.Parent:FindFirstChild("Humanoid") and Players:GetPlayerFromCharacter(hit.Parent) then
if canpass == true then
canpass = false
HotPotatoHolder.Value = Players:GetPlayerFromCharacter(hit.Parent)
HotPotato.Parent = HotPotatoHolder.Value.Character
Highlight.Parent = HotPotatoHolder.Value.Character
task.wait(0.8)
canpass = true
end
end
end)
end)()
This is happening because you are locating the current character at the time, not the next characters. I will try to explain.
Let us say you have two players in a game, Richard and Markus. Richard has the hot potato.
So when you typed, “HotPotatoHolder.Value.Character:WaitForChild("HumanoidRootPart"),” you actually typed this: “HotPotatoHolder.Value.Richard:WaitForChild("HumanoidRootPart").” Even though you set the hot potato’s new holder to Markus, you connected the Touched event to Richard’s root and not Markus’.
What you can do to register the new holders is use an event. Try this:
local connection : RBXScriptConnection
HotPotatoHolder:GetPropertyChangedSignal("Value"):Connect(function()
if connection then -- In case 'connection' has not been used (it's nil).
connection:Disconnect()
end
local player = HotPotatoHolder.Value
local character = player.Character
local rootPart = character.HumanoidRootPart
connection = rootPart .Touched:Connect(function(otherPart)
-- The rest of your code.
-- You may want to include the Humanoid.Died event.
end)
end)
This updates the Touched event for the new holders’ Humanoids (RootParts, sorry).
(Edit) I changed the Humanoid to HumanoidRootPart because I mistook HumanoidRootPart for Humanoid.
Ok I see the problem, I just didn’t know how to fix it. I’ll try it when I get home, but what is instead of the HotPotatoHolder Being an object value it’s a local variable ? How would I detect if the variable is changed?
There is no built-in event or function to detect that. You can create a BindableEvent to do this. However, it would not fire automatically. I will give you some code to write, I just need to get to my laptop.
(Edit) Here is the example code:
local holder: Player
local connection: RBXScriptConnection
local function changeHolder(player: Player)
if not player:IsA("Player") then return end -- (Checks the argument.)
if connection then
connection:Disconnect()
end
holder = player
local character = holder.Character
local rootPart = character.HumanoidRootPart -- Forgot this in the last example.
connection = rootPart.Touched:Connect(function(otherPart)
-- The rest is an example (you can add to this, of course):
changeHolder(Players:GetPlayerFromCharacter(otherPart.Parent))
end)
end
changeHolder(Players.Richard) -- Remember Richard?
I do not think that will help, since the value is the same (it did not change). You can try that, though.
I’m assuming that you asking this question means that you are unable to set the Value property after the event listener. So you can turn the anonymous function connected to HotPotatoHolder:GetPropertyChangedSignal() into a global function, and then call that function outside of the event. Try this:
local function onValueChanged()
if connection then -- In case 'connection' has not been used (it's nil).
connection:Disconnect()
end
local player = HotPotatoHolder.Value
local character = player.Character
local rootPart = character.HumanoidRootPart
connection = rootPart.Touched:Connect(function(otherPart)
-- The rest of your code.
-- You may want to include the Humanoid.Died event.
end)
end
HotPotatoHolder:GetPropertyChangedSignal("Value"):Connect(onValueChanged)
onValueChanged()