Player Not Receiving Coins On-Touch

Hello Roblox developer community! I have run into an issue when developing my game. I am attempting to make a pop it fidget toy simulator game where you get cash for popping bubbles. The bubble is popping fine, and it plays the sound effect, but it doesn’t give the player coins. What am I doing wrong? I am a beginner scripter, so I’m not sure what’s happening.


The Code


The Leaderstats Script:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PopItMoneyStats")

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"

	local Coins = Instance.new("IntValue", Leaderstats)
	Coins.Name = "Coins" 
	Coins.Value = 0

	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Coins.Value = Data.Coins
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {
		["Coins"] = Player.leaderstats.Coins.Value;
	})
end)

script.Parent.Touched:Connect(pop)

Code Inside the Bubble:

local Players = game:GetService("Players")
local function givePoints(player)
	local playerStats = player:WaitForChild("leaderstats")
	local playerPoints = playerStats:WaitForChild("Coins")
	playerPoints.Value = playerPoints.Value + 10
end
local function pop(player)
	local player = Players:GetPlayerFromCharacter(script.Parent.Parent)
	if script.Parent.CanCollide == true then
		script.Parent.Pop:Play()
		script.Parent.Transparency = 1
		script.Parent.CanCollide = false
		givePoints(player)
		wait(4)
		script.Parent.Transparency = 0
		script.Parent.CanCollide = true
	end
end

script.Parent.Touched:Connect(pop)

Thank you for reading, comment down below if you can help!

1 Like

You’re referencing the parent of the character model, which is workspace. So try script.Parent

Same thing happens, no change. Also, just realized it seems to be erroring on the givePoints(player) function.

Reason why is that you’re referencing the model, not the actual player Instance.

here it is winbloo. It’s in here.
@Winbloo

1 Like

Woops, I see it now, my bad :sweat_smile:

Sorry, I am a beginner programmer, not sure what this means. How would I fix this issue?

1 Like

Basically, you’re expecting the argument is a player instance and NOT a character model. When your bubble gets touched, the argument for the part that hits your bubble is gonna be a part, NOT a player. To solve this, you need to reference the character model and get the player using this function.

Players:GetPlayerFromCharacter(character)

1 Like

If you are not clear, I can explain more in-depth.

1 Like

script.Parent.Parent is not getting the player’s character, it’ll get the parent of the character model, so do script.Parent instead, this will get the players character instead of the parent of the character:

local player = Players:GetPlayerFromCharacter(script.Parent)
Just replace ur current line with this ^

It’s still not working, I may have not edited the correct part of the script. Here is the updated script for the bubble:

local Players = game:GetService("Players")
local function givePoints(player)
	local playerStats = player:WaitForChild("leaderstats")
	local playerPoints = playerStats:WaitForChild("Coins")
	playerPoints.Value = playerPoints.Value + 10
end
local function pop(player, character)
	local player = Players:GetPlayerFromCharacter(character)
	if script.Parent.CanCollide == true then
		script.Parent.Pop:Play()
		script.Parent.Transparency = 1
		script.Parent.CanCollide = false
		givePoints(player)
		wait(4)
		script.Parent.Transparency = 0
		script.Parent.CanCollide = true
	end
end

script.Parent.Touched:Connect(pop)

Already tried this, doesn’t work.

This is the problem. Only ONE argument is past to the Touched event. Meaning that your parameter, character, will be nil because it doesn’t get anything. You’ll have to manually do this in your function scope by getting the player argument and get its parent and use the function I told you to use.

1 Like

I disagree with

local player = Players:GetPlayerFromCharacter(script.Parent)

Try this:

local player = Players:GetPlayerFromCharacter(Player.Parent)

It will get the player’s character model.

3 Likes

Thank you for the correction, I read the variables wrong.

You probably should change your code because your variable is the same as the name of the first parameter of the function.

1 Like

This works, thank you so much for your help!

1 Like

You should also thank the me and the other person since we did solve a little of you. Here’s a tip, name your variables correctly so you wouldn’t misunderstood.

2 Likes

Very true. Thank you to @ItzMeZeus_IGotHacked and @Winbloo for your help as well!

1 Like