Scripts not working when locally cloned?

So I have this client in my tool that when equipped, it gives the player a gui after clicking on a player. But when its cloned, the scripts inside of the gui do not work, the script that clones and gives the gui is client, and I would imagine this is the problem.

distance = 5
local player = game.Players.LocalPlayer

script.Parent.Equipped:connect(function(mouse)
	mouse.Button1Down:Connect(function()
		if mouse.Target.Parent:FindFirstChild("Humanoid") and (mouse.Target.Position - script.Parent.Handle.Position).magnitude <distance then
		local Prisoner = mouse.Target.Parent
		local Gui = script.GUI:Clone()
		Gui.Parent = player.PlayerGui 
		Gui.Menu.SentenceMenu.Sentence.MouseButton1Click:Connect(function()
		script.Parent.CuffEvent:FireServer(game.Players:GetPlayerFromCharacter(Prisoner),Gui)
		Gui:Destroy()
		end)
		end
	end)
	end)

Scripts do not replicate when cloned, Use a server script to parent the GUI’s Clone to the playergui

5 Likes

Instead of cloning it over and over you can just change the visible property to true/false.

If the GUI contains only LocalScripts and UI elements, it should be fine. However, if it contains instances that the server must interact with, such as a RemoteEvent or Server Script, it will not work (as the cloned GUI, including everything inside of it, is local).

So I was able to put this together, now im getting an error in the server script saying “Attempt to index local PrisonerPlayer”

LOCAL:

distance = 5
local player = game.Players.LocalPlayer

script.Parent.Equipped:connect(function(mouse)
	mouse.Button1Down:Connect(function()
		if mouse.Target.Parent:FindFirstChild("Humanoid") and (mouse.Target.Position - script.Parent.Handle.Position).magnitude <distance then
		local Prisoner = mouse.Target.Parent
        script.Parent.GiveGui:FireServer(Prisoner)
		end
		end)
	end)

SERVER:

script.Parent.GiveGui.OnServerEvent:Connect(function(player,Prisoner)
local Gui = script.GUI:Clone()
		Gui.Parent = player.PlayerGui 
		Gui.Menu.SentenceMenu.Sentence.MouseButton1Click:Connect(function()
		local PrisonerPlayer = game.Players:GetPlayerFromCharacter(Prisoner.Parent)
		Cuff(PrisonerPlayer,Gui)
		end)
		end)

function Cuff(PrisonerPlayer,Gui)
	local at = Gui.Menu.ArrestTime.Value
	PrisonerPlayer.stats.JailTime.Value = at
	PrisonerPlayer.stats.Arrested.Vaue = true
	PrisonerPlayer.Team = game.Teams.Prisoner
	Gui:Destroy()
end

This is because game.Players:GetPlayerFromCharacter(Prisoner.Parent) is returning a nil value. Print out what Prisoner.Parent is and confirm it is actually the character and not just a Body Part/Hat/Tool.

But I had the client check to make sure it was a player when it looks for Humanoid.

I see the issue:

local PrisonerPlayer = game.Players:GetPlayerFromCharacter(Prisoner.Parent)

Change that to this:

local PrisonerPlayer = game.Players:GetPlayerFromCharacter(Prisoner)

If the Prisoner variable the server receives is the character, then you do not need to pass Prisoner.Parent to GetPlayerFromCharacter, as this would be giving game.Players. Just give it Prisoner.

Therefore, local PrisonerPlayer = game.Players:GetPlayerFromCharacter(Prisoner)

Another question about this whole arrested system, how would I make it so when they join/when they load, the JailTime value goes down 1 every second, I tried it here but it does not work, any help?

game.Players.PlayerAdded:Connect(function(Player)

Player.CharacterAdded:Connect(function(Character)

if Player.stats.Arrested == true then

wait(2)

local colorDone = Color3.new(54,108,162)

Character:WaitForChild("Shirt").ShirtTemplate = "rbxassetid://1833054296"

Character:WaitForChild("Pants").PantsTemplate = "rbxassetid://1855745304"

Character.Head.Title.F.T.TextColor3 = colorDone

Player.Team = game.Teams.Prisoner

while true do

Player.stats.JailTime.Value = Player.stats.JailTime.Value - 1

wait(1)

end

end

end)

end)