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

Why does this tycoon script only work in studio and not online? (click to buy)

Is there any way to fix this script, it's basically a click to buy tycoon script. Working perfectly in studio, can't find the problem - i'm guessing it's something to do with the local player part?


local state = false
upgradecost = 999 --How much is the thing? Add it here.
function act()
state = not state
if state then
	local cash = game.Players.LocalPlayer.leaderstats:findFirstChild("Coins") --Add your money type here.
	if cash.Value > upgradecost then
		cash.Value = cash.Value - upgradecost
		script.Parent.Parent:remove()
		end
	end
end

script.Parent.ClickDetector.MouseClick:connect(act)

1 answer

✓ Accepted answer

This is probably in a Regular Script but you can't use LocalPlayer in anything but a Local Script.

Luckily ClickDetector's "MouseClick" event returns the player that clicked. So you can use that.

local state = false
local upgradecost = 999
-- Local functions and variables are better; more efficient 
local function act(plr)
	state = not state
	if state then
		local cash = plr.leaderstats:FindFirstChild("Coins")
		if cash.Value > upgradecost then
			cash.Value = cash.Value - upgradecost
			script.Parent.Parent:Destroy() -- Use Destroy(), not remove().
		end
	end
end

script.Parent.ClickDetector.MouseClick:Connect(act)
I fixed your tabbing.

Let me know if this works out for you.

1 comment

0

Thanks a lot bro!! it worked ily

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