When the entity spawns in the workspace,it hits the player,it justs prints “waiting for player”,and ain’t going any further(ignore prints).
print("waiting for player...")
local player = game:GetService("Players").PlayerAdded:Wait()
local hui = game.ReplicatedStorage.collectedkanictras
print("player is here!!!11")
script.Parent.hitpart.Touched:Connect(function(hit)
print("JUST PRINT SOMETHING ALREADY")
while wait(0.1) do
print("smth")
if workspace:FindFirstChildOfClass("Model") and hit.Parent:FindFirstChild("Humanoid") and hit.Parent:WaitForChild("Humanoid").WalkSpeed == 13 then
print("aaaa")
hit.Parent:FindFirstChild("Humanoid").Health = hit.Parent:FindFirstChild("Humanoid").Health - 2.75
if hui:FindFirstChildOfClass("Folder") then
if hui:FindFirstChild("kanictra10") then
hui:FindFirstChild("kanictra10").Parent = workspace.parts
player.PlayerGui.qusrt.TextLabel.Text = "0/10"
elseif hui:FindFirstChild("kanictra9") then
hui:FindFirstChild("kanictra9").Parent = workspace.parts
player.PlayerGui.qusrt.TextLabel.Text = "0/10"
elseif hui:FindFirstChild("kanictra8") then
hui:FindFirstChild("kanictra8").Parent = workspace.parts
player.PlayerGui.qusrt.TextLabel.Text = "0/10"
elseif hui:FindFirstChild("kanictra7") then
hui:FindFirstChild("kanictra7").Parent = workspace.parts
player.PlayerGui.qusrt.TextLabel.Text = "0/10"
elseif hui:FindFirstChild("kanictra6") then
hui:FindFirstChild("kanictra6").Parent = workspace.parts
player.PlayerGui.qusrt.TextLabel.Text = "0/10"
elseif hui:FindFirstChild("kanictra5") then
hui:FindFirstChild("kanictra5").Parent = workspace.parts
player.PlayerGui.qusrt.TextLabel.Text = "0/10"
elseif hui:FindFirstChild("kanictra4") then
hui:FindFirstChild("kanictra4").Parent = workspace.parts
player.PlayerGui.qusrt.TextLabel.Text = "0/10"
elseif hui:FindFirstChild("kanictra3") then
hui:FindFirstChild("kanictra3").Parent = workspace.parts
player.PlayerGui.qusrt.TextLabel.Text = "0/10"
elseif hui:FindFirstChild("kanictra2") then
hui:FindFirstChild("kanictra2").Parent = workspace.parts
player.PlayerGui.qusrt.TextLabel.Text = "0/10"
elseif hui:FindFirstChild("kanictra1") then
hui:FindFirstChild("kanictra1").Parent = workspace.parts
player.PlayerGui.qusrt.TextLabel.Text = "0/10"
else
print("no gas cans found.")
end
else
return
end
else
return
end
end
end)
Hello! Can you please provide more context as to where this script is being run from? Along with also when this script is run, is it run upon an event, etc.
If your entity is spawning after the player joins and there is no new player joining, your playerAdded function will yield the script until another player joins, so keep that in mind.
Right, but you’re using playerAdded and waiting for the player to be added, if the player you are looking for is already in the game, it won’t find that player and the script will yield.
From what I understand, you aren’t waiting for someone to join the game, rather looking to see where the player is. If this is wrong please correct me.
playerAdded will only fire upon a new player joining, which player are you looking to access? If you’re looking to access all players, you will have to make a more complicated loop, if you are looking for a defined person, you use game.Players:FindFirstChild(“Username”) to then have access to their player object.
Hello, alot of problems with this code. First I will start with the issue you’ve currently got.
Your code yields while waiting for a player, but for this to run, the player already exists.
Ignoring what is said above you would still have alot of issues.
You are waiting for ANY player to be added, not just a specific or local player.
You are using deprecated methods (wait()). Use task.wait().
You are using a massive amount of elseif. Instead “group” the objects your looking for.
You are searching for object classes, which will give not guarantee the object you are looking for
Here is some improved code, if you have questions please do ask as it is worth learning instead of copy pasting!!!
There are alot of assumsions by me done here so if anything is incorrect and you dont know how to fix it let me know.
-- ASSUMING THAT THIS IS A SERVER SCRIPT
-- Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Objects
local Canister = script.Parent -- assuming thats what this is?
local HitBox = Canister.HitBox
local CollectedKanictras = ReplicatedStorage.collectedkanictras
-- Functions
function ApplyDamage(Humanoid: Humanoid, Amount: number)
if not Humanoid then
return false
end
if typeof(Amount) ~= "number" then
return false
end
Humanoid.Health -= Amount
return true
end
function GetPlayersKanictras(Player)
return CollectedKanictras:FindFirstChild(Player.Name)
end
function OnTouched(Hit: BaseParent)
local Character = Hit.Parent
if not Character:IsA("Model") then
return
end
local Player = Players:GetPlayerFromCharacter(Character)
if not Player then
return
end
local Humanoid = Character:FindFirstChild("Humanoid")
if not Humanoid then
return
end
if not Humanoid.WalkSpeed == 13 then
return
end
ApplyDamage(Humaniod, 2.75)
local Kanictras = GetPlayersKanictras(Player)
if not Kanictras then
return
end
Player.PlayerGui.qusrt.TextLabel.Text = `{#Kanictras:GetChildren()}/10`
end
function Init()
HitBox.Touched:Connect(OnTouched)
end
Init()