I have 2 scripts, 1 works and the other one doesn't. They have exactly the same line of code, but 1 errors and the 2nd one doesn't.They are both inside server scripts. Here they are:

SCRIPT ONE this one does not work

local debounce = true
local IntValueInsideFolder = workspace.ExampleFolder.ExampleFolder.ExampleValue

script.Parent.Touched:Connect(function(hit)
	if not debounce then
		return
	end
	debounce = false
	local user = game.Players:GetPlayerFromCharacter(hit.Parent) -- THIS LINE DOES NOT WORK
	local stats = user:FindFirstChild("leaderstats")
	local PG = user:WaitForChild("PlayerGui",1)
	if stats.ExampleLeaderstatValue.Value < IntValueInsideFolder.Value then
		script.Parent.CanCollide = true
		script.Parent.PopupGui:Clone().Parent = PG
		wait(1)
		PG:WaitForChild("PopupGui",1):Destroy()
		debounce = true
	else
		script.Parent.CanCollide = false
		debounce = true
	end	
end)

SCRIPT TWO this one works

local debounce = true 

function onTouched(hit)
	if not debounce then 
		return
	end
	debounce = false
	local user = game.Players:GetPlayerFromCharacter(hit.Parent) --THIS PART WORKS
	local stats = user:FindFirstChild("leaderstats") 
	if stats ~= nil then 
		local cash = stats:FindFirstChild("Points")
		cash.Value  = cash.Value +20 
		wait(3)
		debounce = true 
	end
end

script.Parent.Touched:connect(onTouched)

Anybody know why this is happening? please answer before i lose my mind

3 comments

0

Try with CanCollide = false. What does output error?

1

With either script, you're not checking whether they're players or not. <-< The function will return the player if what was given it was a player's character, and if not it'll return nil.

0

Yep that worked.. pathetic