Hey Developers,
I made this Script in a Part which works fine! Here is the Script:
script.Parent.Touched:Connect(function(hit)
local Player = game:GetService("Players"):FindFirstChild(hit.Parent.Name)
local PlayerGui = Player.PlayerGui
local RebirthButton = PlayerGui:WaitForChild("RebirthUi").TextButton
local TextButton = PlayerGui:WaitForChild("RebirthUi").TextLabel
if RebirthButton.Visible == false then
TextButton.Visible = true
RebirthButton.Visible = true
PlayerGui:WaitForChild("RebirthUi").RebirthAmount.Value = script.Parent.Amount.Value
Player:WaitForChild("leaderstats").RebirthAmount.Value = script.Parent.Amount.Value
wait(5)
else
TextButton.Visible = false
RebirthButton.Visible = false
wait(5)
end
end)
The Script is in a Part in Workspace. MY FIRST PROBLEM is that when the Gui closes this error in output appears but it still works… Workspace.Rebirth.Script:3: attempt to index nil with ‘WaitForChild’
THE SECOND THING is that I was wondering if I could make a local script to handle the parts (I have exact the same parts in Workspace but they all have different Values… the first problem is that I don’t know how to handle multiple parts with one script and the second problem is that the Values would get changed by the client… Any help would be appreciated!!!
The simplest example of the collection service looks like this, in a single script which on start up gets every object with the tag “RebirthTag” and applies an on touch function named onhitfunction.
for _, taggedPart in ipairs(collectionService:GetTagged("RebirthTag")) do
taggedPart.Touched:Connect(onhitfunction)
end
local RebirthButton = script.Parent.Rebirth
local Information = script.Parent.Information
local debounce = true
for i,v in pairs(game.Workspace:GetChildren()) do
if v.Name == "Rebirth" and v:IsA("Part") then
v.Touched:Connect(function(hit)
if hit ~= nil then
local char = hit.Parent
if char ~= nil then
if debounce == true then
if RebirthButton.Visible == false then
debounce = false
script.Parent.RebirthAmount.Value = game.Players.LocalPlayer:WaitForChild("leaderstats").RebirthAmount.Value
game.ReplicatedStorage.ChangeRebirthAmount:FireServer()
RebirthButton.Visible = true
Information.Visible = true
wait(5)
debounce = true
else
debounce = false
RebirthButton.Visible = false
Information.Visible = false
wait(5)
debounce = true
end
end
end
end
end)
end
end
in the remote event that is fired should give the player the value of the touched part… How do I give the Remote Event that is fired the information tho? Thanks for your fast Reply!!!
I would recommend using the collection service instead of going through every part in the workspace, but if you are going to do that use workspace:GetDescendants() as this will get children of children.
I tested everything and it worked! Thank you so much! the only thing is that the Output spams this error…
Workspace.Rebirth.Script:4: attempt to index nil with ‘WaitForChild’
it comes from the script in the parts…
Yep it works fine now! Only if I touched the Part it spams this error in output:
Workspace.Rebirth.Script:3: attempt to index nil with ‘WaitForChild’
it comes from the line 3 in this script:
script.Parent.Touched:Connect(function(hit)
local Player = game:GetService("Players"):FindFirstChild(hit.Parent.Name)
Player:WaitForChild("leaderstats").RebirthAmount.Value = script.Parent.Amount.Value
end)
script.Parent.Touched:Connect(function(hit)
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
local Humanoid = hit.Parent:FindFirstChild("Humanoid")
if Humanoid then
Player:FindFirstChild("leaderstats").RebirthAmount.Value = script.Parent.Amount.Value
end
end)