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

What is the best way to store the variable of a player?

This script works, if I put the tool into workspace. But when I try to but it into Backpack, I think the problem is how the player is stored, can someone tell me a better solution.

local player = game.Players
Cash = script.Parent.Parent.Parent.Parent.Parent.RealMoney

function onClick()
	if Cash.Value >= 10 then
		Cash.Value = Cash.Value - 10
			local RockLaunch = game.Lighting.Spas12:clone()
			RockLaunch.Parent = script.Parent.Parent.Parent.Parent.Parent.StarterGear
			local RockLaunch = game.Lighting.Spas12:clone()
			RockLaunch.Parent = player.Backpack
			
			
			
			
 
 
	end
end

script.Parent.MouseButton1Click:connect(onClick)

4 comments

0

try putting the variable under the function

0

So like this script.Parent.MouseButton1Click:connect(player)

0

Nope put the cash variable under the function onClick() if needed then add local Cash at line2 

0

the cash isn't the problem its the player

1 answer

✓ Accepted answer

Use RemoteEvents, they will make this so much easier. Put a local script inside the button, put the script inside ServerScriptService, and put a RemoteEvent into ReplicatedStorage, then add the following code:

Local Script:

script.Parent.MouseButton1Click:Connect(function()
	
	game.ReplicatedStorage.RemoteEvent:FireServer()

end)

Server Script:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
	
    local Cash = player.leaderstats:FindFirstChild("RealMoney")
    local Backpack = player.Backpack
	
    if Cash.Value >= 10 then
        Cash.Value = Cash.Value - 10
        local RockLaunch = game.Lighting.Spas12:Clone()
        RockLaunch.Parent = player.StarterGear
        local RockLaunch = game.Lighting.Spas12:Clone()
        RockLaunch.Parent = Backpack

    end
end)

6 comments

0

What should I name the remote event

0

It can be anything, I'd suggest naming it similar to what you're using it for, so if you have multiple RemoteEvents it won't get confusing.

0

It has half worked the money part works but the item isn't put into the players backpack

0

Also, if you're going to change the name of it, make sure to change the "RemoteEvent" inside the scripts to the name you're changing it to.

0

Edited the code.

0

It should work, I don't see a reason for it not to work..

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