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

I MADE THIS SCRIPT TO GIVE A SWORD TO A RANDOM PLAYER WHEN TOUCHED BUT IT DOES NOT WORK?

Asked by 9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

part = script.Parent

serverstorage = game:GetService("ServerStorage")

part.Touched:connect(function(hit)

if game.Players:GetRandomPlayer(hit.Parent) then

p = game.Players:GetRandomPlayer(hit.Parent)

part.BrickColor = BrickColor.new("Really red")

if not p.Backpack:findFirstChild("Sword") then --To check if the player already has a sword. If they don't then the script continues.

local sword = serverstorage:WaitForChild("Sword"):Clone()

sword.Parent = p.Backpack

end

end

end)

0
Edit your post to be a reasonable title and to fix your code formatting to use the Lua code formatting option. BlueTaslem 18071 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

(Updated) Try this:

-- Don't forget to upvote me XD!
local ServerStorage = Game:GetService("ServerStorage")
local PlayerService = Game:GetService('Players')
local Brick = script.Parent

local function OnTouched(hit)
    local Humanoid = hit.Parent:findFirstChild('Humanoid')
    if Humanoid then
        local Number = math.random(#PlayerService :GetChildren())
        local Player = PlayerService:GetChildren()[Number]
        local ToolName = ('Sword')
        local Tool = Player.Backpack:findFirstChild(ToolName )
        print(Player, ' is chosen for random give sword,')

        if not Tool then
            Tool = ServerStorage:findFirstChild(ToolName ):clone()
            Tool.Parent = Player.Backpack
        end -- End If       
    end -- End If
end -- End Sub

Brick.Touched:connect(OnTouched)

Ad

Answer this question