Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I created a script that works in studio but does not work in a normal game, help?

Hello my name is chill22518, I have created a script that is placed under a part. I have called this part 'Anvil'. When a player touches the part, a screen GUI is supposed to pop up. Although this has worked in Studio mode, it does not seem to work as efficiently in a normal game. I have also tried using a local script instead of a normal script, but it still seems to not comply.

Anvil = game.Workspace.Anvil


local function Click(player)
	if player.Parent.Humanoid then
		Player = game.Players.LocalPlayer
		PlayerGui = Player:WaitForChild("PlayerGui")
		Gui = PlayerGui:WaitForChild("CraftGui")
		Gui.Enabled = true
	end
end

Anvil.Touched:connect(Click)

I would appreciate it if you could help me figure out this problem as I have been striving to solve it for a long time.

Kind regards. chill22518

2 answers

You are trying to index the local player through a server-side script. To fix this, you can use a feature of game.Players called "GetPlayerFromCharacter".

I'd also recommend using "local" variables.

local Anvil = game.Workspace.Anvil

local function Click(player)
	if player.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(player.Parent)
		local playerGui = player:WaitForChild("PlayerGui")
		local Gui = PlayerGui:WaitForChild("CraftGui")
		Gui.Enabled = true
	end
end

Anvil.Touched:connect(Click)

1 comment

0

this will error is an NPC or a model with a child called Humanoid touches the anvil as player would be nil.

The first problem is that you cannot use LocalPlayer in a scipt, it only works in a local script as this type of script only runs on the players device so it knows who the player is. A script however runs on the server and is not linked to any player so there is no such thing as a local player to a script.

In my opinion it would be best to use a local script to show the gui but you can also use a server script.

Local script example in StarterPlayerScripts:-

-- we need something about the local player that we can check as this event will fire for all thing / other player that touch the anvil
local plr = game:GetService('Players').LocalPlayer

-- wait for a child called Anvil as the player will be downloading the workspace and it will take a small amount of time for it to be loaded
workspace:WaitForChild('Anvil').Touched:Connect(function(hit)
	-- check the player ie their name, there are other methods of checking the player this is just an example
	if hit.Parent.Name == plrName.Name then
	
		-- find the players gui and enable the gui

		-- we should use FindFirstChild instead of WaitForChild in case the player does not have the gui causing the function to wait infinity  
		local gui = plr:WaitForChild('PlayerGui'):FindFirstChild('CraftGui')

		if gui then -- if we have the gui then enable it
			gui.Enabled = true
		end
	end
end)

We can also use a server script to do the same task but we need to find the player first. we can use GetPlayerFromCharacter to return the player from its character model or nil if no player is found.

Script example:-

workspace.Anvil.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent) -- the parent being the players model

	if plr then -- if we have a player
		-- follow the same process in the above script
		local gui = plr:WaitForChild('PlayerGui'):FindFirstChild('CraftGui')
		
		if gui then -- if we have the gui then enable it
			gui.Enabled = true
		end
	end
end)

Notes

Your script will run each time the event is fired with no delay / cooldown. I would recommend that you include a Debounce.

As this is safe to do on the client side and it would take a small amount of time to replicate the change from the server to the client I would recommend that you use the local script version. It would also cut down on resources required on the server.

This discussion is preserved as it was. New answers, comments, and votes are closed.