You can write your topic however you want, but you need to answer these questions:
Ive got a coin system called Zzzs which an NPC drops when killed and you can collect it to increase your cash. The players are working as a team of only three players in the game - so when collected by one player i want all of the players to get the same increase in coins too.
**What is the issue?
So i have two currencies - one for Zzzs (which the script isnt working for) and one is Jellybeans (where the same method of script is working ). The script for my Zzzs gives the other players Zzzs but not me. I dont understand why because the same method is working for my Jellybeans.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Ive tried different variations of the loop but none of them work. Ive check the devforum to see if anyone has had a similar problem and ive also checked online. I cant find anything to assist me.
My script is below - im wondering if i can manipulate it somehow to give me (local player?) the Zzzs too. Only problem is - i have the game set up to be either 1 player or multiplayer. When i play as 1 player - i get the Zzzs so to put the Zzzs in for a local player aswell as all players- gives me the Zzzs twice when i play as a single player. How can i manipulate it where i can say player.Zzzs.Value = player.Zzzs.Value + part.Value.Value without it giving it to me twice when in single player mode?
local part = script.Parent
local sound = Instance.new("Sound",game.Workspace)
sound.SoundId = "rbxassetid://662290183"
local playersService = game:GetService("Players")
local players = playersService:GetPlayers()
local debounce = true
local function collect (otherPart)
if debounce == true then
local player = game.Players:FindFirstChild(otherPart.Parent.Name)
if player then
debounce = false
for _, plr in ipairs(players) do
local Zzzs = plr:FindFirstChild('Zzzs')
if Zzzs then
Zzzs.Value = Zzzs.Value + part.Value.Value
for i = 0.1, 2.1 do
part.CFrame = part.CFrame + Vector3.new(0, 2.5, 0)
part.CFrame = part.CFrame * CFrame.fromEulerAnglesXYZ(0.0,0.0,0.25)
part.Transparency = 0.5
wait(0.1)
end
wait()
sound:Play()
wait(0.3)
part:Destroy()
end
end
end
end
end
part.Touched:Connect(collect)
So you have a script like this into each part that should be touched for Zzzs and Jellybeans too?
Im gonna guess thats only 1 script, in only 2 parts in workspace, otherwise, you would want to change your approach, by only having 1 script controlling all Zzzs and one for all Jellybeans.
How the Jellybeans script looks like? its changing different values in player right? not the same ones as the Zzzs script?
If you have more than one part that uses this script, I suggest, changing the approach, to have only 1 script that control all the Zzzs touch parts and one for all Jellybeans touch part.
By reading the script without knowing whats the purpose of this, I firstly think on these fixes:
local part = script.Parent
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://662290183"
-- Just reference the player service not the players in server
local playersService = game:GetService("Players")
local debounce = true
local function collect(hitPart)
if debounce then
if hitPart.Name == "HumanoidRootPart" then
local player = game.Players:GetPlayerFromCharacter(hitPart.Parent)
if player then
debounce = false
-- use player service at this moment to get all players in server currently when part touched function is running
for _, plr in ipairs(playersService:GetPlayers()) do
local Zzzs = plr:FindFirstChild('Zzzs')
if Zzzs then
Zzzs.Value = Zzzs.Value + part.Value.Value
for i = 0.1, 2.1 do
part.CFrame = part.CFrame + Vector3.new(0, 2.5, 0)
part.CFrame = part.CFrame * CFrame.fromEulerAnglesXYZ(0.0,0.0,0.25)
part.Transparency = 0.5
wait(0.1)
end
wait()
sound:Play()
wait(0.3)
part:Destroy()
end
end
end
end
end
end
part.Touched:Connect(collect)
Thankyou for your advice. The method that you have shown is as mine works. The beginning of my script for my Jellybeans is pasted below this message. This works for my Jellybeans and xp but it just doesnt work for my Zzzs.
Yes you are right, the jellybeans are a different part which spawns into the workspace from my NPC model when it dies, just like my Zzz parts and the scripts are inside the parts.
Jellybean script…
local function collect (otherPart)
if debounce == true then
local player = game.Players:FindFirstChild(otherPart.Parent.Name)
if player then
debounce = false
for _, plr in pairs(game.Players:GetChildren()) do
local JellyBeans = plr:FindFirstChild('JellyBeans')
local XP = plr:FindFirstChild('XP')
local JB = plr:FindFirstChild('JB')
local xp = plr:FindFirstChild('xp')
if JellyBeans and XP then
JellyBeans.Value = JellyBeans.Value + value.Value
JB.Value = JB.Value + value.Value
xp.Value = xp.Value + value.Value
XP.Value = XP.Value + value.Value
-- Just reference the player service not the players in server
local playersService = game:GetService("Players")
-- use player service at this moment to get all players in server currently when part touched function is running
for _, plr in ipairs(playersService:GetPlayers()) do
Cause you were getting all the players in server when the script run by first time, meaning that players that joined after the script ran would not be taken in count. So, everytime you run that function, you should grab an updated table of the players that are in game right now, not before
Thankyou for this.
It is still only working for other players. I tested the game out with 3 players and it is actually only updating 1 players Zzzs. Its so confusing because its working for my Jellybeans :{