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

GetPlayerFromCharacter() not working?

Asked by 6 years ago

So this script won't work ;-;

REMEMBER to fix the script in a way I can understand. Some professionals like to add extra things to throw me off.

1script.Parent.Touched:Connect(function(dar)
2if game.Players:GetPlayerFromCharacter(dar) then
3local f = Instance.new("Fire")
4f.Parent = dar
5print("hmm ok")
6end
7end)
0
Well, the first thing you should know is that Touched passes the part that touched the part firing the event as its argument. Secondly, you're attempting to make a Fire instance in the part that touched the firing part. DeceptiveCaster 3761 — 6y
0
do dar.Parent for the getplayerfromcharacter because of the reasoning above ^ Scarious 243 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

GENERAL PRACTICE

Use :GetService() to retrieve the Players Service

ISSUES

The parameter of the .Touched event is the part that touched the connecting part. With this in mind, if the player touches this part, then the "dar" is whatever part of the character touched the script's parent, and "dar.Parent" is the Character Model of the Player

The :GetPlayerFromCharacter() function uses the character model to find the player, so you'll need to change the parameter of the :GetPlayerFromCharacter from "dar" (the part) to dar.Parent (the character model)

REVISED SERVER / LOCAL SCRIPT

1script.Parent.Touched:Connect(function(dar)
2    if game:GetService("Players"):GetPlayerFromCharacter(dar.Parent) then
3        local f = Instance.new("Fire")
4        f.Parent = dar
5        print("hmm ok")
6    end
7end)

This script will place fires onto every part that touches it, if you only want a single fire to be created on each part, you can use an if-then to check that there is not already a fire on the part.

SUGGESTED SERVER / LOCAL SCRIPT

1script.Parent.Touched:Connect(function(dar)
2    if game:GetService("Players"):GetPlayerFromCharacter(dar.Parent) then
3        if dar:FindFirstChild("Fire") == nil then
4            local f = Instance.new("Fire")
5            f.Parent = dar
6            print("hmm ok")
7        end
8    end
9end)
0
I thought GetPlayerFromCharacter is the same as saying "if the character model from the character is true then" Am I wrong? Please help :C FearlessX96 74 — 6y
0
You're kind of right, if you intend on using this method, the GetPlayerFromCharacter checks that there is a player associated with the Character, however, normally people check that the touched object's parent contains a Humanoid. SerpentineKing 3885 — 6y
Ad

Answer this question