I have a script here that, when block1 (box) touches block2 (incinerator), it gives the player points in each of their leaderstats and also destroys the block and triggers particles. It all works perfectly just as I want it to…except that I have it fetching the players information through PlayerAdded. The aim of the game is buying blocks and burning them. When you buy a block, it clones from ReplicatedStorage into the workspace. That makes the whole script useless because the block is being added after the player. I’ve tried to use GetPlayers instead, but my trouble there is that I don’t quite understand tables as of yet, and I’m trying to learn. Here is my code inside the block:
local fire = game.Workspace.Incinerator.FireParticle
local sound = game.Workspace.Incinerator.Explode
game.Players.PlayerAdded:Connect(function(player)
local boxes = player:WaitForChild("leaderstats").Boxes
local cash = player:WaitForChild("leaderstats").Cash
script.Parent.Touched:Connect(function(touchingPart)
if touchingPart == game.Workspace.Incinerator then
boxes.Value = boxes.Value + 1
cash.Value = cash.Value + 1
fire.Enabled = true
sound:Play()
wait(0.1)
script.Parent:Destroy()
wait(0.6)
fire.Enabled = false
end
end)
end)
(Note: this is a one player game, and I am using a normal script. I am doing this because it is easier for me as I don’t yet understand local scripts and client sided functions.)
I would greatly appreciate if someone is able to explain to me what I need to change and possibly how it works so I am able to do it on my own in the future. Thank you to anyone who helps, and if you need me to give any more details on what im doing then please ask.
Video of the already existing part working, while the exact copy part that i spawn in, doesnt work.
That does work…however that was not the issue I was having. The issue I was having is that this script is inside of the block that spawns in. since im using a PlayerAdded function, it doesnt work because the block is being added after the player already joins. I’m looking for a way to fetch the players leaderstats without using PlayerAdded. if that makes sense?
local fire = game.Workspace.Incinerator.FireParticle
local sound = game.Workspace.Incinerator.Explode
local player = game:GetService("Players")
while true do
for i, v in ipairs(player:GetChildren()) do
local boxes = v:WaitForChild("leaderstats").Boxes
local cash = v:WaitForChild("leaderstats").Cash
script.Parent.Touched:Connect(function(touchingPart)
if touchingPart.Name == "partnamehere " then
boxes.Value = boxes.Value + 1
cash.Value = cash.Value + 1
fire.Enabled = true
sound:Play()
wait(0.1)
script.Parent:Destroy()
wait(0.6)
fire.Enabled = false
end
end)
end
wait(1)
end
I have an idea that might work if you know how to code it properly.
Instead of
how about you move the part to ReplicatedStorge/ServerStorage, and instead of cloning the brick and putting it in workspace. You just check that it’s in ReplicatedStorage/ServerStorage and just move it to workspace no cloning needed.
Pro’s: All you have to do is move the block around using one script and it doesn’t have to be inside the model.
Con’s: You have a weird system where this is your main script and it deals with everything thus cloning this script inside a model won’t work.
If you’d like a little bit of coding help I can help out with that!
This works perfectly! the one problem i had was that it would hit the part multiple times and give me 4 points instead of one. I just added a simple debounce to that and it fixed the issue. thank you so much! hopefully I can learn more about for i loops from this. I really appreciate it
There’s one very glaring issue with this post, you’re increasing the stats for every player in the game, not just the player of which the blocks belong to.
Upon further inspection I see that this is a single player game (there are still better ways to achieve this).
local players = game:GetService("Players")
local incinerator = workspace.Incinerator
local fire = incinerator.FireParticle
local sound = incinerator.Explode
local block = script.Parent
local debounce = false
block.Touched:Connect(function(touchingPart)
if debounce then
return
end
if touchingPart == incinerator then
debounce = true
local player = players:GetPlayers()[1] --single player game so we can simply index the first item to get the only player instance
local leaderstats = player.leaderstats
local boxes = leaderstats.Boxes
local cash = leaderstats.Cash
boxes.Value += 1
cash.Value += 1
fire.Enabled = true
sound:Play()
wait(0.1)
block:Destroy()
debounce = false
wait(0.6)
sound:Stop()
fire.Enabled = false
end
end)
Without knowing the full game’s system this was as optimised as I could get it.