Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How does this on touched script not work when everything is correct?

Hi, I have a script here:

script.Parent.Touched:Connect(function(hit)
local k = hit.Parent.Name
if hit.Parent:FindFirstChild('Humanoid') then
game.Players[k].Backpack.WHY.Sound:Play()
game.Players[k].Backpack.WHY.Oof:Play()
game.Players[k].Backpack.WHY.Disabled = false
game.Players[k].PlayerGui.ScreenGui.Enabled = true
game.Players[k].PlayerGui.ScreenGui.TextLabel.Font = "Arcade"
wait(0.1)
game.Players[k].PlayerGui.ScreenGui.TextLabel.Text = "Y"
game.Players[k].PlayerGui.ScreenGui.TextLabel.Sound:Play()
end
end)

It's pretty simple. Roblox studio says there's no errors. I double checked every path and made sure it's correct. The capitalization and everything is correct. Its parent, however, is a union, which I think may be causing the problem. I'm honestly not sure though. So, basically, it should work but it doesn't. And yes, I have FilteringEnabled off.

1 answer

✓ Accepted answer

###The problem lies in the fact that you wrongly assume that the touching part will be the character model. Sad to say, this is not always the case. If a part from Workspace touches the UnionOperation, the event fires and you'll get an error.

Workspace is not a valid member of Players

###You should never assume that inside the Players service, an object (Player object, specifically. Do not use this for storage.) with the name of the character model's name will be there.

script.Parent.Touched:Connect(function(hit)
	local k = hit.Parent
       local plr = game:GetService("Players"):GetPlayerFromCharacter(k)
	
	if plr then
		-- code 
	end
end)

###Also, you're modifying the PlayerGui from the server, which cannot be done. ROBLOX has forced FE on games now, so your current code will not work. And please indent your code, it makes it easier to debug and read if indented. Unindented code with errors makes it hard to find errors, such as a missing end statement.

This discussion is preserved as it was. New answers, comments, and votes are closed.