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

Don't know how to do this line of code(keeps saying nil value)(the game is a simulator game)?

local player = game.Players.LocalPlayer

 if script.Parent.MouseButton1Click:connect() then

	local player = game.Players.LocalPlayer
	local strength = player.leaderstats.Strength
	local money = player.leaderstats.Money
	
	money.Value = strength.Value
	strength.Value = strength.Value - strength.Value
	
end

^ This is the line of code At first it was working in a way because it would sell your strength to get money well when ever you sold your strength like 100 you would get 100 cash well when ever you click to add more strength it would go from 0 to 101 after you sold it then, a scripter suggested I used an if then statement and i'm using it but it keeps saying nil value. Help.

3 answers

✓ Accepted answer

Okay. There are 2 problems now. The first, is that we cannot use a if statement for mousebutton1click. Instead, do this:

script.Parent.MouseButton1Click:Connect(function()

end)

The 2nd problem is REALLY IMPORTANT, that could help you forever in Roblox studio. In Roblox, there are two types of view: the "local" view and the "server" view. Local scripts changes what is in the local view and server scripts for server view. So that script you added is surely in a LocalScript. And the Adding Strength function is surely in a Server Script. So they change the same intvalue, but in different views. What you can do is to change that LocalScript into a script, but you cannot do that in GUIs. So here comes those REMOTE EVENTS. They can transfer signals from a LocalScript to a server script, to get the same view. Now, what I suggest to do is to add a REMOTE EVENT in the REPLICATED STORAGE. Name it whatever you want. Now, insert this in the LOCALSCRIPT of the text button:

script.Parent.MouseButton1Click:Connect(function()
        game.ReplicatedStorage:WaitForChild(YOUR REMOTE EVENT NAME HERE):FireServer() --gives a signal
end)

Now, you need a server script to recieve the signal. Insert this in a server script in Workspace:

game.ReplicatedStorage:WaitForChild(REMOTE EVENT NAME HERE).OnServerEvent:Connect(function(player) --getting the signal and the player
        -- SELLING STRENGTH TO MONEY CODE HERE (variable "player" is the player and not his name)
end)

Hope this helped! If it worked, please accept my answer. I wrote all this on my phone for 30 mins, cuz there's no keyboard. Thanks!

That isn't how you use an if statement, here's an example

local var1 = false

if var1 == true then
	print("variable is true")
end

If var1 is set to true then it will print the message, if it is anything else it will skip

Try something like

function leftClick()
	-- Your code --
end

script.Parent.MouseButton1Click:Connect(leftClick)

OR

function script.Parent.MouseButton1Click:Connect()
	-- Your code --
end

I always had some trouble understanding this stuff so if you need me to dissect it more I can

MouseButton1Click is not an if statement...

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:connect(function()

    local player = game.Players.LocalPlayer
    local strength = player.leaderstats.Strength
    local money = player.leaderstats.Money

    money.Value = strength.Value
    strength.Value = strength.Value - strength.Value

end

^ There If this is a simulator game, then there are some other problems, like what if the player had sold 'strength' before? But it just makes the money strength.Value. If you want to fix that then its: money.Value = money.Value + strength.Value

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