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

why does the screen GUI not stay on your screen?

i have a script that when they touch a certain part a GUI appears on their screen. and the same for whe they leave the part but the GUI goes away. When i go to test it, it appears but it then goes away and doesnt come back... and whenever they sit in a chair it goes away. why?

This is a server script in the workspace

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid")then
		if game.Players[hit.Parent.Name].Name == script.Parent.Owner.Value then
			game:GetService("ReplicatedStorage").RemoteEvents.Workplace1JobEvent:FireServer()
		end	
	end
end)
script.Parent.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if game.Players[hit.Parent.Name].Name == script.Parent.Owner.Value then
			game:GetService("ReplicatedStorage").RemoteEvents.Workplace1JobEvent.End:FireServer()
		end
	end
end)

this is a Local Script inside of the player scripts

game:GetService("ReplicatedStorage").RemoteEvents.Workplace1JobEvent.OnServerEvent:Connect(function(plr)
	print(plr)
	local gui = game.Players.LocalPlayer.PlayerGui.ShopGui.Workplace1
	if game.Players.LocalPlayer.PlayerGui.ShopGui.Workplace1.Started.Value == false then
		gui.Started.Value = true
		gui.Visible = true
		gui.Needed.Value = math.random(250,600)
		gui.Current.Value = 0
	else
		gui.Started.Value = true
	end
	return
end)
game:GetService("ReplicatedStorage").RemoteEvents.Workplace1JobEvent.End.OnServerEvent:Connect(function(plr)
	game.Players.LocalPlayer.PlayerGui.ShopGui.Workplace1.Visible = false
	return
end)

if anyone is able to help me with this i would really appreciate it, i have been struggling with it for hours now.

Edit: the problem is with the .TouchEnded part because something is triggering it and im not sure what.

3 comments

0

ok, a few things. you're using OnServerEvent in both the server script and the local script. i'm pretty sure you're only supposed to use OnServerEvent from a server script.

0

so in the local script i should change it to on client event?

0

it's also really hard for me to see what's going on and understand what's happening. i'd recommend keeping the code clean and giving more structural context

1 answer

✓ Accepted answer

###This is happening because OnServerEvent can only be listened for by the server. Use OnClientEvent instead. You're also calling FireServer on the server, which will throw an error. Use FireClient instead.

-- localscript
local plr = game:GetService("Players").LocalPlayer -- use more variables! 
local Remotes = game:GetService("ReplicatedStorage").RemoteEvents -- 

Remotes.Workplace1JobEvent.OnClientEvent:Connect(function() -- no player
    local gui = plr.PlayerGui.ShopGui.Workplace1

    if not plr.PlayerGui.ShopGui.Workplace1.Started.Value then-- use not to check falsey values
        gui.Started.Value = true
        gui.Visible = true
        gui.Needed.Value = math.random(250,600)
        gui.Current.Value = 0
    else
        gui.Started.Value = true
    end
end)

Remotes.Workplace1JobEvent.End.OnClientEvent:Connect(function()
    plr.PlayerGui.ShopGui.Workplace1.Visible = false
end)

###Server:

local Players = game:GetService('Players') 
local Remotes = game:GetService("ReplicatedStorage").RemoteEvents

script.Parent.Touched:Connect(function(hit)
    local plr = Players:GetPlayerFromCharacter(hit.Parent)
    
    if plr then
        if plr.Name == script.Parent.Owner.Value then
            Remotes.Workplace1JobEvent:FireClient(plr)-- player to fire to
        end 
    end
end)

script.Parent.TouchEnded:Connect(function(hit)
    local plr = Players:GetPlayerFromCharacter(hit.Parent)
       if plr and plr.Name == script.Parent.Owner.Value then
            Remotes.Workplace1JobEvent.End:FireClient(plr) 
        end
    end
end)

###On a side note, when you're working with GUIs, always have the client power them. The server should not manipulate GUIs for any reason.

2 comments

0

The script works just fine, but another problem with it is when they leave the part and the GUI goes away, it wont re-appear, ive tried everything to get to re-appear. Do you know why?

0

no errors show up so i have no idea why it doesnt appear

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