Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
Jric0 7
7 years ago
Edited 7 years ago
 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.

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 RubenKan 3615 — 7y

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

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

0
19:46:56 -- LoadCharacter can only be called by the backend server Jric0 7 — 7y
0
Edited DeveloperSolo 370 — 7y
Ad

Answer this question