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

Script dosen't work online why?

So. i am making if you touch a brick then a gui openes but it only works in studio but not online with other players why? and it is a normal script. not a localscript

Workspace. http://imgur.com/a/rTjmb

Here's the current script.

local debounce = false
local isActive = script.Parent.Parent.Job.JobActive

function Shop(hit) 
    if not debounce then
        if hit.Parent:FindFirstChild("Humanoid") then
            		local clone = game.ServerStorage.JobGuis:FindFirstChild("CableMan"):Clone()
            		clone.Parent = game.Players:FindFirstChild(hit.Parent.Name).PlayerGui
					debounce = true
					if isActive.Value == true then
						if script.Parent.Parent.CableMan then
							script.Parent.Parent.CableMan:Destroy()
						end
						game.ServerStorage.ErrorGuis.AlreadyJob:Clone().Parent = game.Players.LocalPlayer.PlayerGui
						wait(2)
						game.Players.LocalPlayer.PlayerGui.AlreadyJob:Destroy()
					end	
            		wait(10)
					debounce = false
			end
		end
	end

 
game.Workspace.Jobs.CableMan.Touched:connect(Shop)

3 comments

0

??

0

You're using a LocalPlayer in a ServerScript. He's asking what type of script this is because you can not access ServerStorage in a LocalScript, and you can not get LocalPlayer in a ServerScript. Also, you changed your title from one bad one to another. Maybe try "Touch Brick Gui Insert Error in DevConsole Here"

0

Well, how am im gonna do this then?

1 answer

✓ Accepted answer

A server script does not have access to the variable LocalPlayer as the script is running on the server.

A local script does not have access the servers storage as it is only for the server to store objects in, if you need both the client and server to use the same objects then you can use ReplicatedStorage.

The reason why this works in solo mode is because you are both the server and the client meaning that both script types can access both server and client resources. It is best to also test by starting a server and not just relying on solo mode tests.

There is a lot which needs to be looked at as you have joined server script and local script functionality which will not work in a live server.

This is an example only, there may be syntax errors as I cannot run this to test.

local debounce = true -- ist best to start at true so you don't need to invert the boolean
local isActive = script.Parent.Parent.Job.JobActive

-- it is a good idea to get the services here
local servStorage = game:GetService('ServerStorage') -- 
local jobGui = servStorage:WaitForChild('CableMan'):Clone() -- stores a clone, alos use wait for child as we know we have the gui
local alreadyJob = servStorage:WaitForChild('ErrorGuis'):WaitForChild('AlreadyJob'):Clone() -- the wait for child stacks so wait fo errorgui then alreadyjob

function Shop(hit) 
    if debounce then
        if hit.Parent:FindFirstChild("Humanoid") then
        
			-- find the player form their character
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
			
			if plr then -- if the player has been found e.g. game.Player.[plr name]
				debounce = false -- now that the player has been found set the denounce
				
				-- you could also check for the gui e.g. wait for child ect
				jobGui:Clone().Parent = plr.PlayerGui -- as we habe a clone we just make a cone of the clone and set the parent to the player gui
				
				if isActive.Value then -- as this is a boolean we dont need to check for a true you were doing true == true which is true and a waste of time
					-- the bit you had before		
					if script.Parent.Parent.CableMan then
	                	script.Parent.Parent.CableMan:Destroy()
					end
					
					-- we create a variable so we can destroy the gui after the wait time instead of looking for it again which is slow
					local tmpGui = alreadyJob:Clone()
					tmpGui.Parent = plr.PlayerGui
					
					wait(2)
					
					tmpGui:Destroy()
				end
				
			end		

            wait(10)
        	debounce = true
		end
	end
end

game.Workspace.Jobs.CableMan.Touched:connect(Shop)

I have changed a lot of the code to remove the localplayer dependencies, please comment if you do not understand why / how this code works. If you do not understand any of the methods I have used please first check the wiki page.

There are also other methods of doing this e.g. in a local script. But I do not know what method is best for you game.

Hope this help.

1 comment

0

Thanks you, it works much better now but it still dosent work online.

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