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

[SOLVED] Refresh script in a gui textbutton not working in-game, works fine in studio.. Why?

 function onClick(mouse)
plr = game.Players.LocalPlayer

script.Parent.MouseButton1Click:connect(function()
	pcall(function()
		local Pos = plr.Character:GetPrimaryPartCFrame()
		plr:LoadCharacter()
		plr.Character:SetPrimaryPartCFrame(Pos)
		if plr.Character:FindFirstChild("ForceField") then
			plr.Character["ForceField"]:Destroy()
		end
	end)
end)
end
script.Parent.MouseButton1Down:connect(onClick)

Works perfectly in studio, but doesn't work in game.

[it is a local script]

I checked the log using F9, and nothing was there.

1 comment

0

It doesn't print anything cuz pcall would catch any errors. You'd have to do: local a,b = pcall(function() -stuff- end) if not a then warn b end

1 answer

✓ Accepted answer

Well, first off that is not how you use pcall.

Whenever you call a pcall function you need 2 arguments, they can be named whatever (I think), but mostly people just do success, and error

so how to do to a pcall

function Good()
	print("hi")
end

local suc, err = pcall(Good)

if suc then
--do stuff
end

Now, looking at your code, it looks like you have no reason for a pcall (correct me if I'm wrong)

P.S you don't need 'mouse' as an argument.

If you are doing this with a gui, having another click function is pointless

[EDIT]

So LoadCharacter only works on the server. How can we fix it? Remote events!

Create a remote event and place it in RemoteStorage Name it whatever, and put the server script wherever?

Inside the script, put this

local re = --path to remote

re.OnServerEvent:connect(function(player)
	local Pos = plr.Character:GetPrimaryPartCFrame()
        player:LoadCharacter()
        player.Character:SetPrimaryPartCFrame(Pos)
        if player.Character:FindFirstChild("ForceField") then
            player.Character["ForceField"]:Destroy()
        end
end)

Inside the local script put

plr = game.Players.LocalPlayer
local re = --path to remote event

script.Parent.MouseButton1Click:connect(function()
        re:FireServer()
end)

Helpful? Make sure to accept this answer! Comments? Questions? Concerns? Make sure to ask!

Did I get anything wrong? Please don't hate, just tell me!

sources Pcall information

2 comments

0

19:46:56 -- LoadCharacter can only be called by the backend server

0

Edited

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