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!
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.
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)
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.
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.