Car Spawn System Error!

I have successfully created a spawn script that allows you to go in proximity of a guy andthen u can spawn a jeep, but when I team-tested it when I went in proximity everyon got the UI up on their screen too! I don’t know why, can anyone help?

My Script:

local folder = game.Workspace.VehicleSpawn1

game.Players.PlayerAdded:Connect(function(plr)
	script.Parent.Touched:Connect(function(hit)
		if plr:GetRankInGroup(33488955) >= 30 then
			plr.PlayerGui.CarSpawn1.Frame.Visible = true
		end
	end)
	script.Parent.TouchEnded:Connect(function(hit)
		plr.PlayerGui.CarSpawn1.Frame.Visible = false
	end)
end)

Let me know what im doing wrong here!

Is this a Local or Server script and where is the Part the script is in located?

It’s best to just print statements to find out what variable values are when checking if statements for troubleshooting purposes:

local folder = game.Workspace.VehicleSpawn1

game.Players.PlayerAdded:Connect(function(plr)
	script.Parent.Touched:Connect(function(hit)
        print(plr , "  ", plr:GetRankInGroup(33488955) )
		if plr:GetRankInGroup(33488955) >= 30 then
			plr.PlayerGui.CarSpawn1.Frame.Visible = true
		end
	end)
	script.Parent.TouchEnded:Connect(function(hit)
		plr.PlayerGui.CarSpawn1.Frame.Visible = false
	end)
end)

script.Parent.Touched:Connect(function(hit)

that line always fires for every player that joined when the part is touched. You need to implement a check to know if the player who touched the part is the player you got from playeradded

How would I do that?
Really need help with this

Would this code work? I have tested it alone and it works, but I do not know cuz my friend went to sleep

local folder = game.Workspace.VehicleSpawn1

game.Players.PlayerAdded:Connect(function(plr)
	script.Parent.Touched:Connect(function(hit)
		if plr:GetRankInGroup(33488955) >= 30 and hit.Parent.Humanoid.DisplayName == plr.DisplayName then
			plr.PlayerGui.CarSpawn1.Frame.Visible = true
		end
	end)
	script.Parent.TouchEnded:Connect(function(hit)
		plr.PlayerGui.CarSpawn1.Frame.Visible = false
	end)
end)
script.Parent.Touched:Connect(function(hit) -- Detects a hit
	if hit:FindFirstChild("Humanoid") then -- Detects if 'hit' has a Humanoid
		local player = game.Players:FindFirstChild(hit.Parent.Name) -- Gets the player
		if player:GetRankInGroup(33488955) >= 30 then -- Gets player group role
			player.PlayerGui.CarSpawn1.Frame.Visible = true -- Enables UI
		end
	end
end)

script.Parent.TouchEnded:Connect(function(hit) -- Don't need to check if in group because it doesn't matter if the don't see the UI
	if hit:FindFirstChild("Humanoid") then
		local player = game.Players:FindFirstChild(hit.Parent.Name)
		player.PlayerGui.CarSpawn1.Frame.Visible = false -- Disables UI
	end
end)

Tell me if this works. I just came up with it in about 5 mins

1 Like

Alright, I am gonna try it in just 1 minute. I’ll let you know!

I just edited it I forgot a .Parent

I ran a local server test with 2 people and nothing happened when I went into the box, no errors in the output. Doesn’t work in solo test either

Give me a second to test this line.

Certainly.
Let me know what u find out!

script.Parent.Touched:Connect(function(hit) -- Detects a hit
	if hit:FindFirstChild("Humanoid") then -- Detects if 'hit' has a Humanoid
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Gets the player
		if player:GetRankInGroup(33488955) >= 30 then -- Gets player group role
			player.PlayerGui.CarSpawn1.Frame.Visible = true -- Enables UI
		end
	end
end)

script.Parent.TouchEnded:Connect(function(hit) -- Don't need to check if in group because it doesn't matter if the don't see the UI
	if hit:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		player.PlayerGui.CarSpawn1.Frame.Visible = false -- Disables UI
	end
end)

try this

Still nothing, I go in proximity and I even made sure that I actually hit the part, but nothing

hm I can’t help anymore atm. I wish you luck trying to fix this.

Yeah, it’s alright. Thank you, migt just break it down entirely and rescript it differently!

Thanks for trying though!

If anyone else can help, it would be very much so appreciated!

You still didn’t answer if this was a server script or a local script.

What are the outputs of the print statements I suggested? I just gave one example, but if you print those variables you can see what values each if statement is reacting to for the section of code you deal with.

Can’t be bothered doing all that. I’ll just watch a tutorial on yt or smthn

if there is a part that detects that, make sure this
(put this script inside that part that detects)

script.Parent.Touched:Connect(function(hit)

local plr = hit.Parent.Name – find the name of the player

local plringame = game.Players:FindFirstChild(plr)

plringame.PlayerGui. – make the gui visible and stuff here

end)