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

Why doesn't this script work in a LocalScript?

I have a script inside a GUI that gives a player ammo for a bow. Here's the script (it's inside a LocalScript):

function clicked()

local player = game.Players.LocalPlayer
local stats = player:findFirstChild("leaderstats")

player:WaitForDataReady()
	
	if stats.Points.Value >= 1 then 
		local arrowValue = player:findFirstChild("arrowValue") -- this is the ammo for the bow
		local longbow = player.Backpack:findFirstChild("Long Bow")
		local longbow2 = game.Workspace:findFirstChild(player.Name):findFirstChild("Long Bow") 
		if longbowz then
			arrowValue.Value = arrowValue.Value + 1
			stats.Points.Value = stats.Points.Value - 1
		elseif longbowz2 then
			arrowValue.Value = arrowValue.Value + 1
			stats.Points.Value = stats.Points.Value - 1
		end
	end
end

script.Parent.MouseButton1Down:connect(clicked)

When put into a normal script and the variable for the player (line 4) is changed to script.Parent.Parent.Parent.Parent.Parent (the player), the script works fine. But, when the script is put into a LocalScript and line 4 is changed to player = game.Players.LocalPlayer, the script doesn't work. The script is a descendant of PlayerGui, so why doesn't it work?

1 answer

✓ Accepted answer

You cannot use WaitForDataReady (or use any other data persistence related methods) from a client-side script, therefore line six is your problem.

1 comment

0

Ah, okay.

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