Need assistance with SurfaceGUI Text Button

I have a regular part with a click detector as a button for a skill point reset, i wanted to turn the button into a surfaceGUI text button, however i’m confused because the script works just fine as a part in work space but it doesn’t work when placed inside starterGUI. at first the error i got was “attempted to index nil with ‘name’.” which I interpreted as player wasn’t defined (which is weird to me since its the exact same code becides the changes from a part to a GUI so why wouldn’t it say the same thing for the part button ?)

local serverFunctions = require(game.ServerScriptService.Functions)
local cd = false

script.Parent.MouseButton1Click:Connect(function(player)
	if cd then return end

	cd = false

	local stats = game.ServerStorage.PlayerData:FindFirstChild(player.Name)
	if stats then
		local skills = stats:FindFirstChild("Skills")
		local rank = stats:FindFirstChild("Rank")
		local skillpoints = stats:FindFirstChild("SkillPoints")
		local spentsp = stats:FindFirstChild("SpentSkillPoints")
		if skills and rank and skillpoints and spentsp then
			for i,v in pairs(skills:GetChildren()) do
				v:Destroy()
			end
			skillpoints.Value = 24
			spentsp.Value = 0
		end
	end

	wait(2)

	cd = false

end)

and so I tried changing the Script into a Localscript to define “player” with

local player = game:GetService(“Players”).LocalPlayer

but couldn’t communicate with ServerScriptService, i assumed i could to get around this with a remote event of some kind but I don’t know where i would start. Can someone explain to me a possible solution im not seeing or point me in the right direction ? any feedback would be helpful

First, create a remote event that’s located inside ReplicatedStorage. You could call it spReset.
Next, create a script. Set its RunContext property to Client. Write this in the script:

local spResetRemote = game.ReplicatedStorage:WaitForChild("spReset")
script.Parent.MouseButton1Click:Connect(function(player)
	if cd then return end

	cd = false

	spResetRemote:FireServer()

	wait(2)

	cd = false

end)

Finally, for your regular server script (RunContext set to Legacy), write this:

local spResetRemote = game.ReplicatedStorage:WaitForChild("spReset")
spResetRemote.OnServerEvent:Connect(function(player)
	local stats = game.ServerStorage.PlayerData:FindFirstChild(player.Name)
	if stats then
		local skills = stats:FindFirstChild("Skills")
		local rank = stats:FindFirstChild("Rank")
		local skillpoints = stats:FindFirstChild("SkillPoints")
		local spentsp = stats:FindFirstChild("SpentSkillPoints")
		if skills and rank and skillpoints and spentsp then
			for i,v in pairs(skills:GetChildren()) do
				v:Destroy()
			end
			skillpoints.Value = 24
			spentsp.Value = 0
		end
	end
end)

Thanks for the help, it works perfectly

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.