Why doesn't the model change players?

local Running = false

local RS = game:GetService("ReplicatedStorage")
local Event = RS.HotPotatoSelected
local Start = RS.PotatoCreateEvent

local Potato = Instance.new("Accessory")
		RS.Handle.Parent = Potato
		local Attatchment = Instance.new("Attachment")
		Attatchment.Name = "BodyFrontAttachment"
		Attatchment.Parent = Potato.Handle
Potato.Parent = RS
Potato.Name = "Potato"

Attatchment.Orientation = Vector3.new(0,0,-90)
	print("Potato Instance has been created")

Event.OnServerEvent:Connect(function()


	local Players = game.Players:GetPlayers()
	local random = Players[math.random(1,#Players)]
	Start.OnServerEvent:Connect(function()
		local PotatoClone = Potato:Clone()
		
		PotatoClone.Parent = random.Character
		print("Potato Instance has been attached to the player")
		Running = true
		while Running == true do
			
			PotatoClone.Handle.Touched:Connect(function(hit)
				if not PotatoClone.Parent then
					PotatoClone.Parent = hit.Parent.Character
					print("Swapped Potato to another player")
					wait(2)
				end
                                     RS.LavaEventServer.OnServerEvent:Connect(function()
					Running = false
				end)
			end)
			wait()
		end
	end)
end)

This Block:

while Running == true do
			
			PotatoClone.Handle.Touched:Connect(function(hit)
				if not PotatoClone.Parent then
					PotatoClone.Parent = hit.Parent.Character
					print("Swapped Potato to another player")
					wait(2)
				end
                                     RS.LavaEventServer.OnServerEvent:Connect(function()
					Running = false
				end)
			end)
			wait()
		end

It’s supposed to check if the player with the potato model attached to them touches another player. When it touches another player, it should transfer the potato model to the player that was hit, except nothing happens.

1 Like

I think that theres no need to constantly Connect the touch event inside the while loop, connecting it once its enough.

Is the handle of the potate enough big to actually Touch another player? the potato is a tool?

The potato has a big enough hitbox. It is not a tool, but a hat. Does that affect anything?

Im not sure if while being an accessory the touch event could not trigger, but, yup, theres a chance.
Why not trying that, when a player gets the potato, connect their character HumanoidRootPart to the touch event, when HRP touches another player, disconnect that, reconnect to the new player, and give the potato.

Meanwhile, I suggest remove the Touch event connection out of the while loop, to connect it just once, and do a print inmediately when the handle touch anything, just to see if at least the touch event is connected

So you’re saying I just make it check if the HumanoidRootPart is touched instead of the Potato hat Model?

Yup, that would be a different approach if the Touching events are not working with the accessory. But, if thats not the problem, then thats not needed, the first important thing is to connect the touch event once, when the player gets the potato and not doing it in a while loop

I made a little test with your script.
Yup, the accessory has no issues to trigger the touch event.

With this little adjustment its working fine for me, I test it with my player and a dummy and it works.
But, I found that the events to start the game are called from Client? I dont think thats a good idea. You are using a remote triggered from client to server to connect another OnServerEvent that gives the potato to the random player. When or how those events are called?
I suggest change that to a server side control

I disabled the lava event just for debugging

Event.OnServerEvent:Connect(function()
	local Players = game.Players:GetPlayers()
	local random = Players[math.random(1,#Players)]

	Start.OnServerEvent:Connect(function()
		local PotatoClone = Potato:Clone()

		PotatoClone.Parent = random.Character
		print("Potato Instance has been attached to the player")

		local cooldown = true
		PotatoClone.Handle.Touched:Connect(function(hit)
			if hit.Name == "HumanoidRootPart" then
				if cooldown then
					cooldown = false
					PotatoClone.Parent = hit.Parent
					print("Swapped Potato to",hit.Parent)
					wait(1)
					warn("cooldown done")
					cooldown = true
				end

			end
			--RS.LavaEventServer.OnServerEvent:Connect(function()
			--Running = false
			--end)
		end)

	end)
end)

This code works! Thank you so much for helping me out. I see what I ended up doing wrong with the code.

1 Like

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