Why does this money giving script not work?

so basically you click the part (not gui) and it gives you money.

local click = script.Parent.ClickDetector

local part = script.Parent

local player = game.Players.PlayerAdded

local playerStats = player:WaitForChild('leaderstats'):WaitForChild('Coins')

click.MouseClick:Connect(function()

part.Position = part.Position + Vector3.new(0, 0, 5)

part.Transparency = 0.5

wait(1)

playerStats.Value = playerStats.Value + 1

part.Transparency = 1

part.CanCollide = false

end)
1 Like

Change that to

local player = game.Players.LocalPlayer

or use a connect funcation and do it on join if you are not using a local script.

1 Like

ive tried that but that didn’t work when i first used it.

1 Like

Then you did something wrong different the first time. @intorsetorpolice1 is correct. Players.PlayerAdded is a signal event, not a player.

Try this script instead:

local part = script.Parent
local click = part.ClickDetector
local isClicked = false

click.MouseClick:Connect(function(Player)
  if not isClicked then
    isClicked = true
    
    local playerStats = Player:FindFirstChild("leaderstats"):FindFirstChild("Coins")
    
    part.Position += Vector3.new(0,0,5)
    part.Transparency = 0.5
    wait(1)
    playerStats.Value += 1
    part.Transparency = 1
    part.CanCollide = false
  end
end

thanks it worked but now how do i make this script work?

this script is supposed to destroy the ball when it hits/touches the part and after that it fires event where the game resets

local ball = game.Workspace:WaitForChild("ball!")

local part = script.Parent

local event1 = game.ReplicatedStorage.RemoteEvent

part.Touched:Connect(function(hit)
	if hit.Parent == ball then
		ball:Destroy()
		event1:FireClient()
	end
end)

I am happy to help… But I need a little more context. Is ball a model? Why is the event being fired? To whom is the event supposed to be fired?

yes the ball is a model and the fire event goes to this script:

so here is how the game works you click the sticks and they give you coins, when the ball hits the part the ball gets destroyed and the sticks go back into the storage of the model.

local event1 = game.ReplicatedStorage.RemoteEvent

local sticks = game.ReplicatedStorage.StickStorage

local stick = sticks:WaitForChild("Stick")

event1.OnServerEvent:Connect(function()
	stick.Parent = script.Parent.sticks
end)

And do the sticks need to be rendered locally, or would server-side stick addition be okay, too?

have you ever played “kerplunk”? well that’s basically what this game is like except with more to it… so the game you click the sticks and the sticks go to a storage in replicated storage to be out of the way so the ball can fall down, when the ball hits/touches the part the part destroys the ball and when that happens the script tells the other script to bring all the sticks back to the storage in the model to be used again.

basically the stick only one person can click it but the other players can’t click their sticks, later ill make a game mode where they can do that but that’s later when I get better at scripting.

Sorry about that I completely misunderstood the topic lol. :smile:

Yeah so I would actually recommend combining the two scripts, considering only one player plays at atime.

local ball = game.Workspace:WaitForChild("ball!")
local part = script.Parent
local sticks = game.ReplicatedStorage.StickStorage
local stick = sticks:WaitForChild("Stick")

part.Touched:Connect(function(hit)
	if hit.Parent == ball then
		ball:Destroy()
		stick.Parent = --sticks holder
	end
end)