Basic Scripting Help

I have this simple script where the player gets cash when they touch a particular part. It doesn’t seem to work properly.

game.Workspace.Tower:WaitForChild("Stage2"):WaitForChild("StartPlatform").Touched:Connect(function(hit)
	local char = hit.Parent
	local player = game.Players:GetPlayerFromCharacter(char)

	local playerCash = player.leaderstats.Drops.Value

	print("hi")
	if not Floor1Won then
		Floor1Won = true
		playerCash += 5
	end
end)
7 Likes

You are changing the playerCash variable by 5. Not the player cash’s value

To change the playerCash’s value, do:

game.Workspace.Tower:WaitForChild("Stage2"):WaitForChild("StartPlatform").Touched:Connect(function(hit)
	local char = hit.Parent
	local player = game.Players:GetPlayerFromCharacter(char)

	local playerCash = player.leaderstats.Drops

	print("hi")
	if not Floor1Won then
		Floor1Won = true
		playerCash.Value += 5
	end
end)

and you can use @TheRealANDRO’s code to make the code more optimized

1 Like

The variable consists of .Value at the end of the Value instance itself.

But it doesn’t work, idk how to explain it so I will use an example:

Let’s say drops has a starting value of 0

local value = Drops.Value
print(value) -- Prints 0
print(Drops.Value) -- Prints 0

value += 5
print(value) -- Prints 5
print(Drops.Value) -- Prints 0

But if you set the value variable to Drops itself:

local value = Drops
print(value.Value) -- Prints 0
print(Drops.Value) -- Prints 0

value.Value+= 5
print(value.Value) -- Prints 5
print(Drops.Value) -- Prints 5
local Stage = game.Workspace.Tower:WaitForChild("Stage2") -- Waiting for parts
local Part = Stage:WaitForChild("StartPlatform")

Part.Touched:Connect(function(hit) -- Detecting when touched
	local character = hit.Parent
    if not character then return end -- Checking if hit.Parent exists

	local player = game.Players:GetPlayerFromCharacter(character)
    if not player then return end -- Checking if the found character actually belongs to a player

	local playerCash = player.leaderstats.Drops

	print("hi")
	if not Floor1Won then
		Floor1Won = true
		playerCash.Value += 5 -- Increasing the value
	end
end)

Make sure Floor1Won actually exists and is a boolean.
Make sure the parts load in correctly.

Alright I’ll try this and let you know.

Isn’t working. I tried it. It still doesn’t increase the value.

Still doesn’t work. Isn’t increasing value.

Does it reach to print("hi")?

No, the hi never printed. (extending words since the 30 alpha limit)

Then make sure the parts load correctly. (Add another print statement to the first line of the function to check if it runs.)
It might be because of that. Also, make sure the code passes if checks as well.

The parts load in 5 seconds later. There’s a loop which changes the tower every 5 minutes.

If the parts get destroyed, then the code might not run after 5 minutes.
Make sure to put the events in a loop.
Also did you add a print statement to the first line of the function as I said?

Alright I’ll try it out and let you know. Give me a minute.

I added a print statement to the first line, it’s somehow not printing.

I kinda got confused cause I haven’t used hit for a really long time

do you get anything on your output?

No. (abcdefghijklmnopqrstuvwxyz)

1 Like

do you get any warning that says InfiniteYield?

Then it’s cause of the parts not loading in.
Does the output mention “Infinite yield possible on…”?

(Make sure game.Workspace.Tower actually exists, if the tower loads in after a couple of seconds, do: game.Workspace:WaitForChild(“Tower”))