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

Why does my coin pick up script give me 0 cash instead of 100?

So basically this script is supposed to be so when you collect a coin, it gives you a certain amount of coins then removes itself. I am trying to make it so if they have a certain upgrade they get more money from the coins but when I collect it, it gives me no cash even though I set my multiplier to 10.

local baseamount = 10
script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if hit.Parent:FindFirstChild("Humanoid") then
		local multvalue = player.extra.multiplier.Value
		local multipler = baseamount * multvalue
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + multipler
		script.Parent:Destroy()
		
	end
end)

6 comments

0

Try printing out what "multipler" is before destroying the script, see if it has a value. Is this a local script?

0

Based on your script if multiplier has no value then 10 x anything is nothing, so it wouldnt work.

0

The multiplier has a value of 10. I set it while in studio. It's not a local script aswell. If I set it using a script when I join it works. How would I make it so that when it changes it will always work?

0

Thats your issue. Changing the value while in studio counts as a client sided change, and will not be recognized by a server script.

0

I also recommend you follow the changes Diezel outlined in his answer, they're all good. Trying changing the players multiplier value inside the script, and it should work correctly.(just for testing)

0

In case i wasnt clear, this is a Filtering Enabled issue, changes made to server created values from the client will not replicate. If the server created the Multiplier then the server MUST change the value.

1 answer

Try this:

local baseamount = 10
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local extraPoints = player:FindFirstChild("extra")
if extraPoints ~= nil then 
local multvalue = extraPoints.multiplier.Value
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + multvalue*baseamount
else
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + baseamount
end
script.Parent:Destroy()
end
end)

I changed some things:

  1. I think the script should check for the humanoid before getting the player from the character.

  2. I made the script respond to both scenarios: If there is an "extra" and if there isn't. If there is, give the player the score times the player's multiplier value. If there's no extra, just give the baseamount.

  3. I put script.Parent:Destroy() outside the "if extra ~= nil" expression. Since it should happen no matter what the scenario.

6 comments

0

It works, but when I change the value it doesn't update and it keeps giving me the multiplier I had when I joined.

0

oops, I meant "extraPoints" at line 6, not "extra". Editing it..

0

It still doesn't update when I pick up the coin.

0

same with line 7, my bad!

0

I just realized, if you edit it manually it doesn't work. It needs to be updated by the server,

0

You just realized? I commented that on your question a while ago.

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