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

LoadCharacter not working when trying to make a refresh character GUI button?

I'm trying to make a button that refreshes the player who clicked it, but it isn't working and I don't know why. There are no errors in the output or anything underlined red in the script. Here is the script:

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)

Any help would be appreciated!

2 comments

0

LoadCharacter cannot be called through a LocalScript. You would need to use RemoteEvents to achieve this. You have no errors because you are wrapping the function in a pcall.

0

don't forget to accept my answer if it helps.

2 answers

✓ Accepted answer

You're calling it from a local script. That won't work. You will need a remote event.

-- # remember to define `remoteEvent`
remoteEvent.OnServerEvent:Connect(function(client)
    client:LoadCharacter()
end)

Here i'm just listening on the server side for OnServerEvent to be fired. When it is, the character of the player who fired the remote gets reloaded.

And from the client you would call remoteEvent:FireServer() to fire the server event.

local function onClick() -- # localise! 
    remoteEvent:FireServer() -- # remember to define `remoteEvent`
end

script.Parent.Activated:Connect(onClick)
-- # Activated is the recommended way of getting GUI click input.

2 comments

0

I'm really new to scripting, so could you tell me how to put it in a script please?

0

call the evnt from a server-side. That's all you need, just do some fitting and refitting, maybe some wiki-browsing here and there.

Let's see if this works... I've seen many people saying that you need to use events, but for a beginning scripter, I wouldn't expect you to understand events. Heck, I don't even understand events yet! So I just do a little local value changing here and a little checking for changes there, and that's all that's needed!

So, let's try a something a bit like this:

To set things up, you need a StringValue in ReplicatedStorage named "PlrName"

In the Client-Side (local, in the Button):

plr = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	game:GetService("ReplicatedStorage"):FindFirstChild("PlrName").Value = plr.Name
end)

-- very short and simple.

In the Server-Side (plain, in ServerScriptService):

local plrNameV = game:GetService("ReplicatedStorage"):FindFirstChild("PlrName")

plrNameV:GetPropertyChangedSignal("Value"):Connect(function() -- checks for the value to change

	local plr = game.Players:FindFirstChild(plrNameV.Value) -- Gets the player from the value set in the LocalScript

	local Pos = plr.Character:GetPrimaryPartCFrame() -- you put - Set a value for the player's position

	plr:LoadCharacter() -- you put - load the player's character
	plr.Character:SetPrimaryPartCFrame(Pos) -- you put- set the player to their original position

	if plr.Character:FindFirstChild("ForceField") then
		plr.Character:FindFirstChild("ForceField):Destroy()  -- Your Remove Forcefield module
	end
end)

2 comments

0

It doesn't seem to be working. What do you mean by in the server side and the client side?

0

I put the descriptions in parentheses. The server-side (run on the server) is run on Roblox, hence it not being "local", and the client-side (player's side) is run on the player's PC, hence it being called a "local" script.

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