Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

Ad

Answer this question